Question

I briefly stepped through some of the SalesRule scenarios and code, and it seems like anything dealing with category params is ignored. On the one hand, this makes sense, as quote items have no concept of categories. On the other hand, why is this an option?

Was it helpful?

Solution

Just keep stepping through. You will get to Mage_SalesRule_Model_Rule_Condition_Product. And even though we are testing a quote item at first we then load the product and continue validating the associated product instead:

/**
 * Validate Product Rule Condition
 *
 * @param Varien_Object $object
 *
 * @return bool
 */
public function validate(Varien_Object $object)
{
    $product = false;
    if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
        $product = $object->getProduct();
    } else {
        $product = Mage::getModel('catalog/product')
            ->load($object->getProductId());
    }

    $product
        ->setQuoteItemQty($object->getQty())
        ->setQuoteItemPrice($object->getPrice()) // possible bug: need to use $object->getBasePrice()
        ->setQuoteItemRowTotal($object->getBaseRowTotal());

    return parent::validate($product);
}

And then where the validation takes place, there is some special handling of the category_ids case in Mage_Rule_Model_Condition_Product_Abstract

    public function validate(Varien_Object $object)
    {
        $attrCode = $this->getAttribute();

        if ('category_ids' == $attrCode) {
            return $this->validateAttribute($object->getAvailableInCategories());

As to why - it is more convenient to select a category than having to select every individual sku.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top