开发者

Magento - Reassign catalog rules after modifying product attributes

开发者 https://www.devze.com 2023-04-04 19:13 出处:网络
I\'m in the process of developing an extension for Magento 1.5.1.0, which allows me to add catalog price rules to products which quantity in stock is reduced to zero. I have added an attribute to my a

I'm in the process of developing an extension for Magento 1.5.1.0, which allows me to add catalog price rules to products which quantity in stock is reduced to zero. I have added an attribute to my attribute-set called auto_discount_active. This attribute is my on/off switch which works as condition for my price rule.

I wrote an Observer that reacts on the events sales_order_place_after and catalog_product_save_before. It's task is to check wether to stock quantity of the current product has been changed and set my custom attribute to on or off.

The method which handles the catalog_product_save_before event works fine. After saving an article in the backend, the price rule becomes (in)active like it should. The code looks like following:

class Company_AutoDiscount_Model_Observer
{
  public function updateAutoDiscount($observer)
  {
    /**
     * @var Varien_Event
     */
    $event     = $observer->getEvent();

    $product    = $event->getProduct();
    $data       = $product->getStockData(); 

    $discount   = $data['qty'] < 1 ? true : false;
    $attributes = $product->getAttributes();
    $attribute  = $attributes["auto_discount_active"];

    if ($product->getAutoDiscountAllowed())
    {
        $product->setAutoDiscountActive($discount);
    }

    return $this;
  }
}

Now I want to do the same thing, if someone places an order in my shop. That for I use the event sales_order_place_after which works so far. But after changing the custom attributes value, the price rules are not updated. My observer method looks like this:

public function updateAutoDiscountAfterOrder($observer)
{
    /**
     * @var Varien_Event
     */
    $event      = $obs开发者_StackOverflow社区erver->getEvent();
    $order      = $event->getOrder();

    foreach ($order->getItemsCollection() as $item) 
    {
        $productId  = $item->getProductId();
        $productIds[] = $productId;
        $product    = Mage::getModel('catalog/product')->setStoreId($order->getStoreId())->load($productId);
        $data       = $product->getStockData(); 

        $discount   = $data['qty'] < 1 ? true : false;

        if ($product->getAutoDiscountAllowed())
        { 
            $product->setAutoDiscountActive($discount);
            $product->save();
        }

        Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);
    }
    return $this;
}

After placing an order and saving the bought article manually in the backend without changes, the price rule gets updated. But I have get the update working in my observer method.

What do I have to do to get the catalog price rule being assigned, after changing the custom attribute?

Thx in advance!


Okay, I want to advise you on some fairly major code optimisations.

You can reduce your collection size and remove the conditional logic inside your loop by using:

$order->getItemsCollection()->addFieldToFilter('is_in_stock', 0);

You could also update all the attributes with a much faster method than save(), by using:

Mage::getSingleton('catalog/product_action')
    ->updateAttributes($order->getItemsCollection()->addFieldToFilter('is_in_stock', 0)->getAllIds(), array('auto_discount_active' => 1), 0);

Also, bear in mind, you'll also need to apply your observer to any product stock level modification, ie. product save, import, credit memo (refund) - so its a fairly expansive area. You would probably be better served rewriting the stock class, as there isn't too many events dispatched that will give you enough scope to cover this.

Finally, to perform the assignation of rules, I would suggest extending the resource model for the rule (Mage/CatalogRule/Model/Mysql4/Rule.php) so that you can pass in your array of product ids (to save it iterating through the entire catalogue).

You could simply extend getRuleProductIds() to take a Mage::registry variable (if set) with your product ids from the collection above. Then after running the code above, you could just execute

Mage::getModel('catalogrule/rule')->load(myruleid)->save();

Which will re-index and apply rules to new products as necessary - for only the products that have changed.

I would imagine this method cutting overheads by an extremely significant amount.

0

精彩评论

暂无评论...
验证码 换一张
取 消