dopgl80062 2013-03-28 17:02
浏览 19
已采纳

Magento 1.7中的重新排序/侧边栏显示了隐形产品 - 适用于早期版本

I have a plugin with a virtual product, that is set to VISIBILITY_NOT_VISIBLE. It's a virtual product.

However, it is displayed in the widget "My orders" - but only in Magento 1.7. It works well in 1.6 and 1.5. I have tested in Mage/Sales/Block/Reorder/Sidebar.php in the getItems() method and it has indeed the correct VISIBILITY_NOT_VISIBLE-state.

  1. Has something changed between magento 1.6 to 1.7 in that regard? I can spot difference in the codebase, but not something that may be the cause of this behavior.

  2. May this be a bug, as it only appears in 1.7?

  3. Can I bypass this without touching the original codebase?

  • 写回答

1条回答 默认 最新

  • duanfu7840 2013-04-03 10:48
    关注

    You are correct. I'm not sure if this is bug ( but as the functionality wise this should be bug ).

    So the difference between 1.6 ( and older version ) / and 1.7 is:

    The Model

    Mage_Sales_Model_Order

    Has This method

    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 
                    }
                }
    
    }
    

    So In the previous code the last ordered item collection has property called 'product' that hold the product object if its returned and its not set if its not returned.

    The above code is fine and works no issues in it.

    In Model

    Mage_Sales_Block_Reorder_Sidebar

    The getItems Method is :

    /**
     * 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;
    }
    

    ================================================================================= In Magento 1.6 and prior versions

    When you call $item->getProduct(); in sidebar block,

    it will return the object property ( Mage_Catalog_Model_Product - product instance - ) that has been set in the method above ( _getItemsRandomCollection() )

    OR it will return NULL if not set.

    So if it returns Product object the item will be shown in the sidebar block and if not the return array will not contain that Virtual Item

    ================================================================================= In Magento 1.7

    When you call $item->getProduct(); in sidebar block,

    ===>> This method is implemented in the model

    Mage_Sales_Model_Order_Item

    When you call the method it will be executed from the model ( it won't lookup for object property ).

    So if you take a look at the method implementation

    /**
     * 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'
    }
    

    So In the case if the method ( _getItemsRandomCollection() ) didn't set the object property 'product' because its not visible or something ( This method will assign it by product ID directly without checking for visibility or anything else.

    Yes I consider this a bug and it need to be fixed

    That was the answer for the reason why

    =================================================================================

    The solution is pretty simple

    1.Create custom module.

    2.Extend/Rewrite the Model.

    Mage_Sales_Model_Order_Item

    3.Override the method and ( Comment the code Block) or add The visibility check in the method.

    Solution 1 ( comment the code block that set the item product ).

    /**
     * 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');
    }
    

    OR

    Solution 2 : Add the visibility check to the method as below.

    /**
     * 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');
    }
    

    I have spent some time doing this :) hope it helps you !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图