Don't Be a Dunce, Save Your Orders

There are some gotchas that you think that you will always see coming. One such gotcha is the need to save an object to the datastore to persist any changes you may have made to that object.

While it seems like a reasonable concept at the base level, there are times that the need to save an object completely escapes your mind. It seems that for many non-developers, this occurs when they have been working a long time on a file, typically a Microsoft Word document, shortly before their computer blue screens, losing all of their work.

In this case, it luckily had nothing to do with Microsoft Word or a blue screen. Instead, I was trying to programmatically add a comment to an order in Magento. I kept triggering the code that should add the comment over and over, unable to find the comment on the order, and noticing that there is nothing logged to any of the Magento log files. Finally, after spinning my wheels for a bit, and checking with a co-worker, it became obvious that the issue was that I was not saving the order, ensuring the comment that I just successfully added to the order was lost forever.

In case you wanted to know how to properly add a comment to an order without notifiying the customer or allowing the customer to see the comment on the frontend, here it is:

$order->addStatusHistoryComment('Order Comment Here', false)
    ->setIsVisibleOnFront(false)
    ->setIsCustomerNotified(false);
comments powered by Disqus

Related Posts

301 Redirecting in Varnish

In Magento, you can set your secure and non-secure URLs explicitly. This works as expected in most cases, but can cause some issues when you have to specify full URLs or need to make any AJAX requests. When using the Nexcess Turpentine extension to enable Magento and Varnish to work together and you wish to only support traffic at www.example.com and not example.com, you would need to enable the setting in the Turpentine module to normalize the host.

Read More

Saving Products in Magento Deletes Tier Pricing

Magento’s framework makes many things simple to accomplish when working with the products and categories of your eCommerce website. However, I have found one scenario that doesn’t exactly work as expected. Tier Pricing in Magento Magento allows you to setup custom pricing levels based upon the quantity purchased. You set the minimum quantity purchased to enable the lower price when the part is added to the customer’s cart. You could see where losing the tiered pricing for an entire catalog of products would be a big deal for a site.

Read More

Unintended Consequences with Magento Observers on Sales Orders

Anyone that uses Magento to place orders will be hard-pressed to consider this process a speedy one. While it takes a while to process the order under the best of circumstances, there are a few things that you can do that actually make it worse. One of those things that can make it worse is creating an observer that runs in the middle of the saving of the order processes that is always slow-running, or continues to get slower over time as the data that the Magento site grows.

Read More