gpt4 book ai didi

php - 我可以在 quote->getAllItems() 上调用哪些方法?

转载 作者:行者123 更新时间:2023-12-02 05:39:57 24 4
gpt4 key购买 nike

事实证明,谷歌搜索是不可能的,如果有人能帮我解决这个问题,我将不胜感激。我是 Magento 的新手,所以甚至可能有关于此的官方文档。

当我有以下内容时,即获取报价对象,然后对其调用 getAllItems(),我在哪里可以看到我可以在 getAllItems() 上调用的所有方法?

$cart = Mage::getModel('checkout/cart')->getQuote();

foreach ($cart->getAllItems() as $item) {
$productId = $item->getProduct()->getId();
$productPrice = $item->getProduct()->getPrice();
}

即我已经看到了 getId() 等,但我还能使用哪些其他方法?

此外,如果我想在报价对象中包含自定义数据,我应该在哪里创建自定义 get 方法来访问这些数据?

谢谢保罗

最佳答案

获取类方法

如果你只是想知道你可以在$item上调用哪些方法,使用

var_dump(get_class_methods($item));

获取类方法的列表。

识别类文件

如果你想知道哪个文件包含$item的类定义,首先使用

var_dump(get_class($item));

获取类名(在本例中为 Mage_Sales_Model_Quote_Item)。

然后将所有下划线 (_) 替换为斜线 (/) 并将 .php 附加到它。这将为您提供类文件的相对文件路径(在本例中为 Mage/Sales/Model/Quote/Item.php)。

现在检查文件夹

app/code/local/
app/code/community/
app/code/core/
lib/

按照相对路径给出的顺序。第一个匹配项通常是您想要的类文件。

创建访问自定义报价属性的方法

你不必。 Magento 会为您做这件事。

与许多其他 Magento 模型一样,引用对象基于 Varien_Object。后者提供了一个_call()方法,PHP的magic methods之一。 :

public function __call($method, $args)
{
switch (substr($method, 0, 3)) {
case 'get' :
//Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$data = $this->getData($key, isset($args[0]) ? $args[0] : null);
//Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
return $data;

case 'set' :
//Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$result = $this->setData($key, isset($args[0]) ? $args[0] : null);
//Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
return $result;

case 'uns' :
//Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$result = $this->unsetData($key);
//Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
return $result;

case 'has' :
//Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
//Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
return isset($this->_data[$key]);
}
throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
}

现在,如果您想在报价对象中包含自定义数据,只需调用

$quote->setMyCustomAttribute(911);

在这样的调用中,PHP 将检查引用对象中定义的方法 setMyCustomAttribute()。如果它找不到这样的方法,它会检查对象是否有一个神奇的 __call() 方法并调用这个方法。

在我们的示例中,将匹配 __call()set 大小写。

现在发生的是,Magento 首先将驼峰式字符串 setMyCustomAttribute 转换为带下划线的键 my_custom_attribute。然后它将值 911 存储到 protected 属性 Varien_Object::_data['my_custom_attribute']

要从报价对象中读​​取自定义数据,您只需调用

$value = $quote->getMyCustomAttribute();

原理是一样的:getMyCustomAttribute 被转换为 my_custom_attribute 并且 Varien_Object::_data['my_custom_attribute'] 的当前值将被退回。

这就是所有的魔法。

请注意,这只是暂时的。如果您希望能够保存/加载您的自定义属性,您需要扩展报价对象。但那是另一回事了。

关于php - 我可以在 quote->getAllItems() 上调用哪些方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com