【问题标题】:Magento order category products by SKU and words in product nameMagento 按 SKU 和产品名称中的单词订购类别产品
【发布时间】:2015-01-13 15:49:13
【问题描述】:

我收到一个客户的奇怪请求。

他们希望按 SKU 以编程方式订购他们的产品,但无论 SKU 是什么,一定有三种产品(三个畅销书)总是首先出现。

所以它会是: 1.产品1 2.产品2 3.产品3 然后是 SKU 订购的其余产品。

我知道下面的代码将按 sku 订购产品,但我不知道如何更改它以实现我想要的。

将通过在产品名称中提及一个词来选择这三种产品,例如 King Size White Bed 中的“White”

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('sku')
    ->setOrder('sku', 'asc')->load();

谁能给我一些帮助吗?谢谢

【问题讨论】:

    标签: magento magento-1.8


    【解决方案1】:

    我以前见过这种行为。它通常会有所不同,通过加载他们试图追加销售的畅销书或其他固定产品的集合,然后加载正常的产品集合以进行分页。集合的这种功能将被封装在块中,以使其在前端更好。会更适合你吗?

    /**
     * Get Best Sellers
     */
    /** @var Mage_Catalog_Model_Resource_Product_Collection $bestSellersCollection */
    $bestSellersCollection = Mage::getModel('catalog/product')->getCollection();
    $bestSellersCollection
        ->addFieldToFilter('name', array('like' => '%white%'))
        ->setPageSize(3)
        ->setOrder('name', Varien_Data_Collection::SORT_ORDER_ASC);
    
    $bestSellerIds = array();
    foreach ($bestSellersCollection as $bestSeller) {
        $bestSellerIds[] = $bestSeller->getId();
    }
    
    /**
     * Get Products excluding the best sellers.
     */
    /** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
    $productCollection = Mage::getModel('catalog/product')->getCollection();
    $productCollection->addFieldToFilter('entity_id', array('nin' => $bestSellerIds))
        ->setOrder('sku', Varien_Data_Collection::SORT_ORDER_ASC);
    

    如果您一心只想拥有一个集合,那么您可能会幸运地合并以下两个答案中的逻辑。

    Magento: how to merge two product collections into one?

    Magento - Collection Filter by Array Keep Order

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多