title: Magento - Reassign catalog rules after modifying product attributes
body: <p>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 <code>auto_discount_active</code>. This attribute is my on/off switch which works as condition for my price rule.</p>
  
  <p>I wrote an Observer that reacts on the events <code>sales_order_place_after</code> and <code>catalog_product_save_before</code>. 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.</p>
  
  <p>The method which handles the <code>catalog_product_save_before</code> event works fine. After saving an article in the backend, the price rule becomes (in)active like it should. The code looks like following:</p>
  
  <pre><code>class Company_AutoDiscount_Model_Observer
  {
    public function updateAutoDiscount($observer)
    {
      /**
       * @var Varien_Event
       */
      $event     = $observer-&gt;getEvent();
  
      $product    = $event-&gt;getProduct();
      $data       = $product-&gt;getStockData(); 
  
      $discount   = $data['qty'] &lt; 1 ? true : false;
      $attributes = $product-&gt;getAttributes();
      $attribute  = $attributes["auto_discount_active"];
  
      if ($product-&gt;getAutoDiscountAllowed())
      {
          $product-&gt;setAutoDiscountActive($discount);
      }
  
      return $this;
    }
  }
  </code></pre>
  
  <p>Now I want to do the same thing, if someone places an order in my shop. That for I use the event <code>sales_order_place_after</code> which works so far. But after changing the custom attributes value, the price rules are not updated. My observer method looks like this:</p>
  
  <pre><code>public function updateAutoDiscountAfterOrder($observer)
  {
      /**
       * @var Varien_Event
       */
      $event      = $observer-&gt;getEvent();
      $order      = $event-&gt;getOrder();
  
      foreach ($order-&gt;getItemsCollection() as $item) 
      {
          $productId  = $item-&gt;getProductId();
          $productIds[] = $productId;
          $product    = Mage::getModel('catalog/product')-&gt;setStoreId($order-&gt;getStoreId())-&gt;load($productId);
          $data       = $product-&gt;getStockData(); 
  
          $discount   = $data['qty'] &lt; 1 ? true : false;
  
          if ($product-&gt;getAutoDiscountAllowed())
          { 
              $product-&gt;setAutoDiscountActive($discount);
              $product-&gt;save();
          }
  
          Mage::getModel('catalogrule/rule')-&gt;applyAllRulesToProduct($productId);
      }
      return $this;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>What do I have to do to get the catalog price rule being assigned, after changing the custom attribute?</p>
  
  <p>Thx in advance!</p>