-
16-10-2019 - |
题
我发现 magento 有两种不同的方式来处理特价,但我不知道哪种方式最适合我的情况。我发现目录价格规则非常强大,但我不知道如何在不硬编码规则 ID 的情况下循环它们。
我的客户需要两种特价:正常折扣(某些产品可享受 20-30% 的折扣)和持续较短时间(1-2 天)的“闪购”。我需要让它们显示在主页和类别页面中,主页中的内容需要由管理员从后端选择,类别页面中的内容也需要选择,每个内容将设置 4 个产品页面(类别页面中的页面需要来自该类别)。然后我有一个页面链接,其中包含所有特价产品。
交易的“选择”部分我真的不知道该怎么做,但到目前为止我什至无法让具有目录价格规则的产品显示在我的主页上。我为他们准备了一个块,我找到了这段代码:
$rule = Mage::getModel('catalogrule/rule')->load(1);
$rule->setWebsiteIds("1"); $productIdsArray = $rule->getMatchingProductIds();
$productsCollection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect("*") ->addAttributeToFilter("entity_id", array("in", $productIdsArray));
$this->setProductCollection($collection);
但我猜 ->load(1);
正在硬编码促销的 ID,如果用户创建新的价格规则怎么办?如何循环遍历所有包含规则的产品?
我确实知道如何遍历特价产品,也许我可以将它们用于闪存促销,但我认为我会将其与我需要实现的其余功能复杂化。
另外,如果您对如何处理用户需要选择 4 款特色产品的问题有任何建议,我将非常感激。
解决方案
也许考虑以下设置:
应用正常折扣(某些产品可享受 20-30% 的折扣)
要么对每个产品手动执行此操作,要么创建一个名为“促销商品”的类别,并在该类别下为每个折扣等级创建一个子类别,例如“20%”、“30%”等。然后,您可以对每个类别使用目录价格规则来获得所需的折扣。
限时抢购
再次,为闪购创建一个类别,然后创建一个对该类别中的产品进行折扣的目录价格规则。您可以在此价格规则上设置“结束”和“开始”日期。
在主页上显示销售产品
为了显示具有特殊价格的产品,我通常创建一个名为的新块 Namespace_Module_Block_Product_List_Special
(特别.php)。需要延长 Mage_Catalog_Block_Product_List
. 。然后您可以覆盖 _getProductCollection()
. 。这是整个块文件:
<?php
class Namespace_Module_Block_Product_List_Special extends Mage_Catalog_Block_Product_List
{
//default item limit
protected $_defaultItemLimit = 4;
public function _construct() {
parent::_construct();
}
/**
* Retrieve product collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
protected function _getProductCollection()
{
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayStartOfDayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('special_from_date', 'desc')
;
$itemLimit = $this->getItemLimit();
$collection->setPageSize($itemLimit);
return $collection;
}
public function getItemLimit() {
if($this->hasData('item_limit')) {
return $this->getData('item_limit');
}
return $this->_defaultItemLimit;
}
}
要使用它,您需要将其添加到您的 config.xml
在下面 <global>
标签:
<blocks>
<modulename>
<class>Namespace_Module_Block</class>
</modulename>
<catalog>
<rewrite>
<product_list_special>Namespace_Module_Block_Product_List_Special</product_list_special>
</rewrite>
</catalog>
</blocks>
然后将其显示在主页上,如下所示:
{{block type="catalog/product_list_special" name="homepage.products.special" template="catalog/product/list.phtml"}}
或者通过布局 XML:
<block type="catalog/product_list_special" name="hometabs.products.special" template="catalog/product/list.phtml"/>
您可以使用相同的方法来显示每个类别中的特殊产品,但这需要更改 _getProductCollection()
检查当前类别的方法。看一下 Mage_Catalog_Block_Product_list::_getProductCollection()
有关用于此目的的代码的一些想法。