4

I have a custom module, which uses the sales_order_place_before observer. I have a set of code in there which scans through the products, and adds items to the order, where necessary. However, I have found that this is running twice, so wherever it adds an item to the order, it does so twice.

I can't work out why it is running twice, so I was going to use a Magento registry value, set to false, and when it has ran once, change it to true, and wrap an if statement around my code, which check that value. In the custom module, I only have the config.xml file and the Observer.php. Where do I put the

Mage::registry('check_sales_order_before', false);

code? I don't think I can put it in the observer, or it will just reset every time, and cause the exact same issue. I'm assuming I add another file somewhere in my module, or place it in a central file somewhere.

Any ideas?

Thank you.

3 Answers 3

6

A little about the registry...

Mage::registry('check_sales_order_before', false);

This is a read operation. It will check if the registry key with check_sales_order_before is set, if so it returns it otherwise null.

To write to the registry, you need to use register():

Mage::register('check_sales_order_before', false);

Please note that by default Magento will throw an exception if you try to register a variable more than once in a session (e.g. if you want to change false to true later on). In your case it's best to utilize the "graceful" argument to ensure it's done silently:

Mage::register('check_sales_order_before', false, true);
// do stuff...
if (...) {
    Mage::register('check_sales_order_before', true, true);
}

Then finally, once your checkout has been completed:

Mage::unregister('check_sales_order_before');

Your implementation

Where do I put the ... code?

Well you don't actually need to initialize this value. Just check if it's set before you do anything, then set it to true once you've done something:

if (Mage::registry('have_added_products_already')) {
    // do nothing, it's already been done...
    return;
}
// do stuff, it hasn't been done yet...
Mage::register('have_added_products_already', true, true);
1
  • Thanks Robbie, just realised my code was wrong/not working as expected in all circumstances. This is the correct way of doing it. Thank you for pointing that out to me.
    – Shaun
    Commented Sep 8, 2016 at 1:57
3

Been looking at this code for too long, but just realised the most obvious way to do it, is to put it in a constructor method at the top of the observer file:

public function __construct()
    {
        Mage::registry('check_sales_order_before', false);
        if (Mage::registry('check_sales_order_before')) {
            Mage::log("\n - registry value is false ", null, 'custom_log.log');
        } else {
            Mage::log("\n - registry value is true ", null, 'custom_log.log');
        }

    }

After this, I set the registry file to true at the end of the if statement, that was wrapped around the code I only wanted to run once. Worked a treat!

Thought I would put it here, in case anyone else was having a similar issue.

3

Adding To The Registry:

The method to add to the registry is as follows. It takes two parameters. The first is the unique key you would like to give your registry entry. The second is the data you are wanting to store.

Mage::register('name-of-registry-key', $your-data);

Getting Data From The Registry:

When you are ready to retrieve the data you have stored, use the following method:

$var = Mage::registry('name-of-registry-key');

Removing From The Registry

When and if you are ready to remove the data from the registry, there is a method to unregister it. This may be important to you, especially if you are using the registry in a loop and you are wanting to set the same registry key name more than once. The Mage::register() method will not let you set a key if it is already set.

Mage::unregister('name-of-registry-key');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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