我需要一个产品集合,其中包含当前特价和/或符合任何促销价格规则的产品。我目前正在网站的其他地方使用 getFinalPrice() 执行类似的操作,但我无法将其添加为过滤器,可以吗?
我有两个独立的代码块,它们或多或少地分别完成了我想要的事情,我想它们应该以某种方式组合在一起:
$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));
和
$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');
代码过滤促销规则需要促销规则 ID,但我真的对所有促销规则感兴趣,而不是某个特定规则。
谁能帮我把这些整合到一起吗?
谢谢,
~gpcola
2011 年 10 月 11 日更新:
好的,这就是我的最终结果:
// 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;
但这没有返回任何产品...我有大约 1000 种产品符合一个或另一个促销价格规则,所以我知道这是不正确的。有什么想法吗?
哦,如果我 print_r($this->_productCollection) 我得到以下信息:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection Object
( [_flatEnabled:protected] => 数组 ( [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
(
...
看起来合适吗?
要获取所有规则,请使用集合:
$collection = Mage::getModel('catalogrule/rule')
->getResourceCollection();
然后对其应用
addWebsiteFilter($websiteIds)
方法。然后你必须循环遍历它并获取匹配的产品 ID(就像你在代码中所做的那样),并且你可以将它们与类似的内容合并:
$merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds());
最后一行(合并内容)也应该可以将目录规则 ID 与特价 ID 合并。
HTH