All about Magento E-commerce Store.......MagentoForum

Tuesday, July 12, 2011

Magento: Order State and Order Status

I built a module a couple of weeks ago for a client who needed to be able to change the status of orders at any time, as well as having a couple of custom order statuses. It appeared that the best way to change the status or state was by using the setState() method on the order object. Before we get into that, let’s take a look at how Magento sets up the default states and statuses:
Magento sets them up in this file in the Sales model config file: app/code/core/Mage/Sales/etc/config.xml under global > sales > order. There in the ‘statuses’ tag, you’ll see the 8 default statuses (some of which you probably didn’t know existed). In the ‘states’ tag, you’ll see that Magento is setting up 7 different order states. There is also a ‘statuses’ tag as a child of the states. Here is where which statuses are able to be a part of each state. For example, under the ‘processing’ state, the only available status is ‘processing’ (as you can see, you can set the default status for any particular state by adding default=”1″ to the status).
Knowing which statuses are a part of which states is very important. When using the setState() method, you need to make sure that the status/state you are trying to change the order to. Another important thing to take note of is that the ‘complete’ and ‘closed’ states are protected, and you cannot use setState() to change an order to either of those states without overriding the order model to allow the passing of false (setState() passes your parameters to a protected function _setState() which hard-codes true into the ‘shouldProtectState’ parameter).
Ok, now on to the setState() method. This method not only allows you to change the state and status, it also adds the change to the order’s Comments History and allows you to notify the customer. This method can take up to 4 parameters, which I’ll list in order:
  • $state (string): This is the state you would like to change the order to. Must match the xml tag in config.xml
  • $status (string or true): This is the status you would like to change the order to. Since by default Magento only has one status per state it either has to be the correct status name, or if you pass true it will just get the default status for the state you are passing.
  • $comment (string): If you want to pass in a string to add to the comments, you can do so. By default it just passes a blank string
  • $isCustomerNotified (bool): Leave this parameter off if you do not want the customer to be notified, otherwise pass in true
So, if I wanted to change the order status to “Cancelled”, and I don’t want the customer notified, this is how I would do it:
$order->setState('cancelled', true, 'Cancelled order due to bad credit card');
Other statuses and states can be added by creating your own module and adding them via your config.xml. You can also add statuses to the other states. This would allow you to change the statuses of your orders right from the order view in the admin.

Some custom Blocks to help you show products

I have a few custom blocks I've written / copied and tweaked from various posts on Magento's forums. I noticed they are pretty universal in how they grab, filter and return a product collection to be used in various template files (.phtml files). The blocks below should all work from List.phtml (app\design\frontend\default\default\template\catalog\product\list.phtml).

These are Block files and are most appropriately used within the Catalog/Product area. (See my post on creating a custom module – these files won't be overwriting any other class but can stand alone).

Run Magento Code Outside of Magento

Hi All -
Finally time for a new post! (Something I'm surprised I haven't covered yet).
This post will inform you on how to run Magento code outside of Magento. All you need is to have access to Magento's 'app/Mage.php' file.
This will be handy code for a few things:

What’s in a block? – Some Magento “basics”

Many developers are familiar with the MVC (Model View Controller) design pattern that is seemingly ubiquitous throughout web frameworks. Looking through the code in Magento, however, we see many other components besides the M's the V's and the C's.
Each module  (a "module" meaning the separate directories within the "app/code/core/Mage" directory that comprise of Magento's different functional areas) contains the usual Controller and Model. You'll see that within each module, there are no Views (more on this later). You'll also see extra tidbits, such as "helper" , "etc" and "sql". These are (and are not) standard within the ZendFramework context and will not be discussed in this article. In these modules are also the sort of files which we work with very often. The all powerful block! This article will attempt to (hopefully accurately) describe just what a Block is and how it's used.

Bestseller module (with Toolbar!)

How many people were disappointed to install Magento's test data and find out that the home page "Best sellers" was just pain HTML placed into the CMS home page? I certainly was one of those people. That's why I decided to create a Bestseller Module that was dynamic and harnessed the power of Magento's built in features. This post shows you the code and gives and explanation of what is happening.
For those of you impatient to get to the code, here it is:

Adding and removing javascript / css when and where you need it

Adding and removing javascript and css is handled separately within Magento. CSS is added in the usual fashion, where you have a <link rel="stylesheet"… />. However, any included javascript (unless linked to "by hand" from a theme's skin) is pulled via a php files which reads through the "js" folder in the root directory (root/js/index.php is responsible for this).
That is all well and good for Magento. The real question is how we, as developers, add these items when we need them. How you as a developer add css or javascript is, luckily, handled the same.
In this post, we will show how to add and remove javascript and css to a CMS page (and anywhere else) that you may need.

Changing Product Tabs from JavaScript to CSS

Search engines typically only read visible page content. This poses a problem with some Magento Themes product view page. With Magento themes (built around the Modern theme) the Product Description, We Also Recommend, Additional Information, and Product Tabs are embedded using JavaScript. While some feel this is a more visually appealing look, the search engines don't.
Amazon.com is a great example of an ecommerce site that uses visible page promotional content to its marketing advantage. Product pages on Amazon are LONG and nothing is hidden behind tabs or embedded in JavaScript product tabs. This leaves more promotional content for the search engines to index. Having the product page content indexed is a must for any ecommerce site hoping to take advantage of organic search.