Magento默认情况下不支持此功能。相反,我们覆盖了Mage_SalesRule_Model_Validator并创建了销售规则重写。在那我们检查关联的产品是否匹配销售规则。
应用程序/代码/本地/AAA/SalesRule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<AAA_SalesRule>
<version>0.1.0</version>
</AAA_SalesRule>
</modules>
<global>
<models>
<aaa_salesrule>
<class>AAA_SalesRule_Model</class>
</aaa_salesrule>
<salesrule>
<rewrite>
<validator>AAA_SalesRule_Model_SalesRule_Validator</validator>
</rewrite>
</salesrule>
</models>
</global>
</config>
应用/代码/本地/ AAA / SalesRule /模型/SalesRule/Validator.php
private function _hasChildInCart($product) {
$quote = Mage::getSingleton('checkout/session')->getQuote();
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
$childrenIds = $this->_getChildrenIds($childProducts);
foreach ($quote->getAllItems() as $item) {
if (in_array($item->getProductId(), $childrenIds)) {
$registeredItem = Mage::registry('rule_config_product_' . $product->getId());
if ($registeredItem != null && $registeredItem->getId() != $item->getId()) {
Mage::unregister('rule_config_product_' . $product->getId());
}
if ($registeredItem == null) {
Mage::register('rule_config_product_' . $product->getId(), $item);
}
return true;
}
}
return false;
}
private function _getChildrenIds($childProducts) {
$childrenIds = array();
foreach ($childProducts as $child) {
$childrenIds[] = $child->getId();
}
return $childrenIds;
}