I need a product collection consisting of products where the special_price is current and/or they match any promotional price rule. I'm doing something similar elsewhere in my site at the moment using getFinalPrice() but I can't add that as a filter, can I?
I've got two separate blocks of code that individually more or less do what I want and I guess they should fit together somehow:
$rule = Mage::getModel('catalogrule/rule')->load(9);
$rule->setWebsiteIds("1");
$productIdsArray = $rule->getMatchingProductIds();
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect("*")->addAttributeToFilter("entity_id", array("in", $productIdsArray));
and
$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);
$collection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
$collection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
The code filtering promo rules requires a promo rule id but I'm really interested in ALL promo rules rather than one specific one.
Can anyone help me fit these together into one please?
Thanks,
~gpcola
Update 10/11/11:
Ok so here is what I've ended up with:
// get catalogue price rule promotion product collection
$storeId = Mage::app()->getStore()->getId();
$rules = Mage::getModel('catalogrule/rule')
->getResourceCollection()
->addWebsiteFilter(Mage::getModel('core/store')->load($storeId)->getWebsiteId());
foreach($rules as $rule) {
$promoids = array_merge($promoids, $rule->getMatchingProductIds());
}
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$pricecollection = Mage::getResourceModel('catalog/product_collection');
$pricecollection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
// get special price product collection
$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);
$pricecollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($pricecollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($pricecollection);
$pricecollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
;
// merge the collections
$merged_ids = array_merge($pricecollection->getAllIds(), $promoids);
$this->_productCollection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('entity_id', $merged_ids)
->setPageSize($this->getProductsCount())
->setCurPage(1);
return $this->_productCollection;
But this is returning no products... I have around 1000 products that match one promo price rule or another so I know that's incorrect. Any ideas?
oh by the way if I print_r($this->_productCollection) I get the following:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection Object
( [_flatEnabled:protected] => Array ( [1] => 1 )
[_productWebsiteTable:protected] => catalog_product_website
[_productCategoryTable:protected] => catalog_category_product
[_addUrlRewrite:protected] =>
[_urlRewriteCategory:protected] =>
[_addMinimalPrice:protected] =>
[_addFinalPrice:protected] =>
[_allIdsCache:protected] =>
[_addTaxPercents:protected] =>
[_productLimitationFilters:protected] => Array
(
)
[_productCountSelect:protected] =>
[_isWebsiteFilter:protected] =>
[_priceDataFieldFilters:protected] => Array
(
)
[_map:protected] => Array
(
[fields] => Array
(
[price] => price_index.price
[final_price] => price_index.final_price
[min_price] => price_index.min_price
[max_price] => price_index.max_price
[tier_price] => price_index.tier_price
[special_price] => price_index.special_price
)
)
[_storeId:protected] => 1
[_itemsById:protected] => Array
(
)
[_staticFields:protected] => Array
(
[entity_id] => entity_id
[type_id] => type_id
[attribute_set_id] => attribute_set_id
)
[_entity:protected] => Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Flat Object
(
[_storeId:protected] => 1
[_resources:protected] => Mage_Core_Model_Resource Object
(
[_connectionTypes:protected] => Array
(
[pdo_mysql] => Mage_Core_Model_Resource_Type_Db_Pdo_Mysql Object
(
[_name:protected] =>
[_entityClass:protected] => Mage_Core_Model_Resource_Entity_Table
)
)
[_connections:protected] => Array
(
[core_read] => Varien_Db_Adapter_Pdo_Mysql Object
(
[_transactionLevel:protected] => 0
[_connectionFlagsSet:protected] => 1
[_ddlCache:protected] => Array
(
[1] => Array
(
...
Does that look right?
to fetch all rules, use the collection:
$collection = Mage::getModel('catalogrule/rule')
->getResourceCollection();
then apply the addWebsiteFilter($websiteIds)
method on it. Then you'll have to loop through it and get the matching products ids (as you're doing in your code), and you can merge them with something like:
$merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds());
This last line (the merging stuff) should work also to merge the catalogrules id's with the special prices ones.
HTH