【问题标题】:sorting and pager not working on custom collection Magento排序和寻呼机不适用于自定义集合 Magento
【发布时间】:2015-07-21 13:37:35
【问题描述】:

您好,我在 magento 中添加了一个名为附件的新产品关系。

就像我有一个主要产品 A 并且我将 B c 和 d 附加为 配件

现在在前端我需要将此集合显示为相同的设计 在列表 .phtml

所以我决定在列表 .phtml 上呈现自定义集合

为此,我在_getProductCollection() 中呈现了一个自定义集合

通过下面的代码

if($_GET['product_id'])
        {

            $productid=$_GET['product_id'];
            $product = Mage::getModel('catalog/product_link')
            ->getCollection()
            ->addFieldToFilter('link_type_id', 6)
            ->addFieldToFilter('product_id',$productid)
            ->load();
            $LinkedProduct=$product->getData();
            foreach($LinkedProduct as $LinkedProducts)
            {
            $LinkedProductId[]= $LinkedProducts['linked_product_id'];   
            }
            $productIds = array_values($LinkedProductId);

            $_productCollection=Mage::getModel('catalog/product')
            ->getCollection()  
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', array('in' => $productIds))
            ->load();
            $this->_productCollection=$_productCollection;

    }else{




        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            // if this is a product view page
            if (Mage::registry('product')) {
                // get collection of categories this product is associated with
                $categories = Mage::registry('product')->getCategoryCollection()
                    ->setPage(1, 1)
                    ->load();
                // if the product is associated with any category
                if ($categories->count()) {
                    // show products from this category
                    $this->setCategoryId(current($categories->getIterator()));
                }
            }

            $origCategory = null;
            if ($this->getCategoryId()) {
                $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }
            $this->_productCollection = $layer->getProductCollection();

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }
}
        return $this->_productCollection;
    }

The collection is working fine

但问题是排序和工具栏不起作用

。您能否建议我如何使排序和寻呼机与我的自定义集合一起使用

【问题讨论】:

    标签: magento sorting collections pagination


    【解决方案1】:

    好的,我已经通过在集合中添加一个排序函数来解决这个问题,如下面的代码

        $productid=$_GET['product_id'];
        $product = Mage::getModel('catalog/product_link')
        ->getCollection()
        ->addFieldToFilter('link_type_id', 6)
        ->addFieldToFilter('product_id',$productid)
        ->load();
        $LinkedProduct=$product->getData();
        foreach($LinkedProduct as $LinkedProducts)
        {
        $LinkedProductId[]= $LinkedProducts['linked_product_id'];   
        }
        $productIds = array_values($LinkedProductId);
    
        $_productCollection=Mage::getModel('catalog/product')
        ->getCollection()  
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id', array('in' => $productIds))
        ->addAttributeToSort($_GET['order'],$_GET['dir'] )
        ->setPageSize($_GET['limit'])
        ->load();
        echo $_productCollection->getSelect() ; 
        $this->_productCollection=$_productCollection;
    

    排序不起作用,因为集合中没有添加排序功能

    ->addAttributeToSort($_GET['order'],$_GET['dir'] )
            ->setPageSize($_GET['limit'])
    

    现在一切正常。

    谢谢

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多