Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:43:16+00:00 2026-05-31T08:43:16+00:00

I’m in the process of developing an extension for Magento 1.5.1.0, which allows me

  • 0

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      = $observer->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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:43:17+00:00Added an answer on May 31, 2026 at 8:43 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.