Magento - 创建一个SALE产品系列,考虑到special_price&促销价格规则

时间:2011-11-06 02:08:29

标签: magento collections product

我需要一个产品系列,其中包含special_price为最新产品和/或符合任何促销价格规则的产品。我正在使用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

更新10/11/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
                                            (
...

看起来不错吗?

1 个答案:

答案 0 :(得分:1)

获取所有规则,使用集合:

$collection = Mage::getModel('catalogrule/rule')
        ->getResourceCollection();

然后在其上应用addWebsiteFilter($websiteIds)方法。然后你必须遍历它并获得匹配的产品ID(正如你在代码中所做的那样),你可以将它们合并为:

$merged_ids = array_merge($collection1->getAllIds(), $collection2->getAllIds());

这最后一行(合并的东西)也应该用于合并目录规则id和特殊价格。

HTH