- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
事实证明,谷歌搜索是不可能的,如果有人能帮我解决这个问题,我将不胜感激。我是 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/
我正在尝试做一些非常简单的事情 - 从订单中获取商品。 Magento 1.7 似乎为此提供了三个功能。 getAllItems() :这会返回所有项目两次。退回的元素类型简单(不可配置) getIt
我正在尝试做一些非常简单的事情 - 从订单中获取商品。 Magento 1.7 似乎为此提供了三个功能。 getAllItems() :这会返回所有项目两次。退回的元素类型简单(不可配置) getIt
事实证明,谷歌搜索是不可能的,如果有人能帮我解决这个问题,我将不胜感激。我是 Magento 的新手,所以甚至可能有关于此的官方文档。 当我有以下内容时,即获取报价对象,然后对其调用 getAllIt
事实证明,谷歌搜索是不可能的,如果有人能帮我解决这个问题,我将不胜感激。我是 Magento 的新手,所以甚至可能有关于此的官方文档。 当我有以下内容时,即获取报价对象,然后对其调用 getAllIt
我正在编写一个观察器来检查订单中的每个项目,在某个时候我得到这些项目 foreach($order->getAllItems() as $item){ //do something
我是一名优秀的程序员,十分优秀!