【发布时间】:2015-01-28 15:56:29
【问题描述】:
我正在进行分配工作,我需要取消分配以前产品的类别并需要动态分配畅销产品(前 20 名)。我为此使用以下代码:-
$_productCollection = Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addAttributeToSort("entity_id","DESC")
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
->setPageSize($productCount);
// Delete Existing Mapped product from Category
$delQuery = 'Delete from catalog_category_product where category_id ='.$catId;
$writeConnection->query($delQuery);
foreach($_productCollection as $product){
$categoryIds = $product->getCategoryIds();
if (!in_array($catId, $categoryIds)) {
$write->query("replace into
`catalog_category_product` (category_id,product_id,position)
VALUES (?,?,0)",array($catId,$product->getEntityId()));
}
}
$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_product');
$process->reindexAll();
$category->save();
代码工作正常,但是一旦将动态产品分配给该类别产品的特定类别后端排序(默认产品列表排序依据),该类别产品就停止工作了。我怀疑这可能是因为我的每个替换产品的位置都是 0查询但还不确定。
如果有人有任何建议,那将非常有帮助。
【问题讨论】:
标签: magento sorting categories