SKU的Magento订购类别产品和产品名称中的字样

时间:2015-01-13 15:49:14

标签: magento magento-1.8

我有一个客户的奇怪请求。

他们想要通过SKU以编程方式订购他们的产品,但是无论SKU如何,都会首先出现三种产品(三种畅销产品)。

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

我知道下面的代码会通过sku订购产品,但我不知道如何更改它以达到我想要的效果。

通过在产品名称中提及单词来选择这三种产品,例如' White'在特大号白色床

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

有人可以给我一些帮助吗?感谢

1 个答案:

答案 0 :(得分: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

相关问题