Magento:需要根据目录价格规则获取产品集合 [英] Magento: Need to get product collection on catalog price rule applied
本文介绍了Magento:需要根据目录价格规则获取产品集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了应用了特殊价格的产品集合,但是我不知道在应用目录价格规则时如何获取产品集合.
I've create a product collection with the special price applied but i dont know how to get the product collection when catalog price rule is applied.
下面是获取特价产品的代码,
Below is the code to get special price products,
public function getSpecialPriceProducts()
{
$categoryID = $this->getCategoryId();
if($categoryID)
{
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID); // this is category id
$collection = $category->getProductCollection();
} else
{
$collection = Mage::getResourceModel('catalog/product_collection');
}
$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->addAttributeToSort('created_at', 'desc');
$collection->addStoreFilter()
->addAttributeToSelect(array('name', 'price', 'short_description','image','small_image','url_key'), 'inner');
$collection->addAttributeToFilter('special_price', array('gt' => 0));
$collection->addAttributeToFilter('special_to_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_from_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
return $collection;
}
有人可以指导我如何获取应用目录价格规则的产品列表吗?预先感谢.
can anyone pls guide me how to get product list where catalog price rule is applied? Thanks in advance.
推荐答案
我已经回答了.
public function getSpecialPriceProducts()
{
$categoryID = $this->getCategoryId();
if($categoryID)
{
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID); // this is category id
$collection = $category->getProductCollection();
} else
{
$collection = Mage::getResourceModel('catalog/product_collection');
}
$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->addAttributeToSort('created_at', 'desc');
$collection->addStoreFilter()
->addAttributeToSelect(array('name', 'price', 'short_description','image','small_image','url_key'), 'inner');
$collection->addAttributeToFilter('special_price', array('gt' => 0));
$collection->addAttributeToFilter('special_to_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_from_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
$rules = Mage::getResourceModel('catalogrule/rule_collection')->load();
// read: if there are active rules
if($rules->getData()) {
$rule_ids = array(); // i used this down below to style the products according to which rule affected
$productIds[] = array(); // this will hold the ids of the products
foreach($rules as $rule) {
$rule_ids[] = $rule->getId();
$productIds = $rule->getMatchingProductIds(); // affected products come in here
}
// merge the collections: $arr is an array and keeps all product IDs we fetched before with the special-price-stuff
$arr = $collection->getAllIds();
if($productIds) {
// if there are products affected by catalog price rules, $arr now also keeps their IDs
$arr = array_merge($arr,$productIds);
}
// we initialize a new collection and filter solely by the product IDs we got before, read: select all products with their entity_id in $arr
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array('name', 'price', 'short_description','image','small_image','url_key'), 'inner')
->addAttributeToFilter('entity_id',array('in'=>$arr))
->load();
}
return $collection;
}
这篇关于Magento:需要根据目录价格规则获取产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文