你是对的。我不确定这是否是错误(但就功能而言,这应该是错误)。
所以 1.6(和旧版本)/和 1.7 之间的区别是:
该模型
Mage_Sales_Model_Order
有这个方法
protected function _getItemsRandomCollection($limit, $nonChildrenOnly = false){
$(...)->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds()) // This is Visibility check ( its fine )
foreach ($collection as $item) {
$product = $productsCollection->getItemById($item->getProductId());
if ($product) {
$item->setProduct($product); // Set Object Data ( 'product' ) if it exists
}
}
}
因此,在前面的代码中,最后订购的项目集合具有名为“产品”的属性,如果产品对象返回,则该属性保存产品对象,如果未返回,则不设置。
上面的代码很好,并且没有问题。
在模型中
Mage_Sales_Block_Reorder_Sidebar
getItems 方法是:
/**
* Get list of last ordered products
*
* @return array
*/
public function getItems()
{
$items = array();
$order = $this->getLastOrder();
$limit = 5;
if ($order) {
$website = Mage::app()->getStore()->getWebsiteId();
foreach ($order->getParentItemsRandomCollection($limit) as $item) {
if ($item->getProduct() && in_array($website, $item->getProduct()->getWebsiteIds())) {
$items[] = $item;
}
}
}
return $items;
}
==================================================== ===============================
在 Magento 1.6 和之前的版本中
当你调用$item->getProduct(); 在侧边栏块中,
它将返回已在上述方法( _getItemsRandomCollection() )中设置的对象属性( Mage_Catalog_Model_Product - 产品实例 - )
或者,如果未设置,它将返回 NULL。
因此,如果它返回 Product 对象,则该项目将显示在侧边栏块中,如果不是,则返回数组将不包含该虚拟项目
==================================================== ================================
在 Magento 1.7 中
当你调用$item->getProduct(); 在侧边栏块中,
===>>该方法在模型中实现
Mage_Sales_Model_Order_Item
当您调用该方法时,它将从模型中执行(它不会查找对象属性)。
所以如果你看一下方法实现
/**
* Retrieve product
*
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
if (!$this->getData('product')) { // If no object attribute set with name ('product')
$product = Mage::getModel('catalog/product')->load($this->getProductId()); // it will always return product instance with that product id.
$this->setProduct($product); // set it to the current sidebar item
}
return $this->getData('product'); // return the object property 'product'
}
因此,如果方法( _getItemsRandomCollection() )没有设置对象属性“产品”,因为它不可见或某些东西(此方法将直接按产品 ID 分配它,而不检查可见性或其他任何东西。
是的,我认为这是一个错误,需要修复
这就是为什么
==================================================== ================================
解决方案非常简单
1.创建自定义模块。
2.扩展/重写模型。
Mage_Sales_Model_Order_Item
3.重写方法和(注释代码块)或在方法中添加可见性检查。
解决方案1(注释设置项目产品的代码块)。
/**
* Retrieve product
*
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
/** comment from here
*if (!$this->getData('product')) {
* $product = Mage::getModel('catalog/product')->load($this->getProductId());
* $this->setProduct($product);
*}
* Till here.
*/
return $this->getData('product');
}
或者
解决方案 2:将可见性检查添加到方法中,如下所示。
/**
* Retrieve product
*
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
if (!$this->getData('product')) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
if(!$product->isVisibleInSiteVisibility()) // add visibility check here
{
$this->setProduct($product);
}
}
return $this->getData('product');
}
我花了一些时间做这个:)希望它对你有帮助!