4

I have a configurable product that has 12 associated products. One of those associated products needs to be part of a promotion.

Let's say the SKU for the configurable is J001 and the SKU for the simple product is J001-2D.

The attribute in the promotion is Container Size. The configurable doesn't have that attribute, but the associated products do.

If I create a shopping cart price rule that looks like the following:

promotion

That doesn't match anything. If I exclude everything (IE, Container Size Is Not whatever), it matches everything in the cart. This says to me that the promotion isn't matching the simple associated product.

Is this intended behavior? Is there a workaround?

  • Wouldnt this work out of the box. I mean cart rules validate the simple products - the problem we have is the opposite. We need to validate the parent attributes instead of the simple ones .... was this behavior changed in Magento? – snh_nl Jun 15 '18 at 8:34
5

Apparently Magento does not support this by default. Instead, we overrode Mage_SalesRule_Model_Validator and created a salesrule rewrite. In that we check to see if the associated product matches the sales rule.

app/code/local/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>

app/code/local/AAA/SalesRule/Model/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;
}

* * *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.