[英]Magento. Sort products collection by position
我有產品收藏。
$_products = Mage::getModel('catalog/product')->getCollection();
在目錄->管理類別->類別產品的管理面板中,我有每種產品的位置。 我如何按位置對$ _products進行排序?
如果要按類別$ category_id中的位置對產品進行排序。 您可以使用以下內容
//Load the category model
$category = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSort('position', 'ASC');
$products_list = $category->getData();
您將獲得按類別$ category_id中的位置排序的所有產品
您需要添加類別,在其中設置產品的位置,作為收集的過濾器
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category);
$collection->addAttributeToSort('position', 'ASC');
我創建了單獨的擴展程序,該擴展程序創建了名稱為“ position”的新產品屬性。 在此擴展中,我創建了一個觀察器,該監聽器保存了偵聽類別。 因此,當管理員在觀察者中保存類別時,我會獲得每個職位並為每個產品設置屬性“職位”。 最后,我可以按“位置”(product)屬性對產品集合進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.