Programmatically Assign Sort Order To Products In a Category

Merchandising Optimization Matters

sort order to products

One of the most critical aspects to running an effective and successful eCommerce site is merchandising. Unlike physical retail locations, merchandising for eCommerce sites can be a little tricky. Details are the key to get consumers to buy, and between the product images you upload and all the categories you place said products in, it all can get a little convoluted. What you need to be concentrating on is how visitors to your online store are able to sort your products within key categories. This will facilitate an ease of use that will make finding, and purchasing top products a breeze. You want all your newest, or most effective, top-selling products to be the first in line to be viewed by potential customers. Therefore your sort order to products, within a category, matters a lot! Today, I’m going to show you how to do this on the product listing pages – programmatically.

Related: [Mini Tutorial] – Extend the Magento API

First and foremost:

sort order to products

You need to ensure the SKU you want to merchandise already exists in Magento. You cannot create your product using this method (although you can assign a product to a category using this method).

The API function you’re using is:

catalogCategoryAssignProduct

The arguments:

SessionId – The value returned to you when you login.
CatId – The CategoryId of the Category you’d like to assign/update the product in.
Product – This is the identifier of the product, we enjoy using SKU as the identifier, Magento also accepts ProductId.
Position – The Sort Order of the product on this particular category listing page.
Product identifier – What identifier you’re using in the “Product” argument. Will likely be either “SKU” or null (for productId)

Related: How To Boost Sales Using Magento’s Rule-Based Product Relations

In the Backend

Getting the categoryId is easy in the backend, but much trickier in code (which I’ll be covering in my next post – so stay tuned!).

Go to Catalog -> Categories -> Manage Categories. Click down your category tree until you get to the category you want. When it loads up the ID will be displayed in plain site right beside the name of your category on the header.

There really isn’t a whole lot else to the functionality of getting this going. It’s pretty straightforward and barebones. The naming of the method is a little sketchy though and doesn’t do well to describe it’s functionality fully.

There are a few headaches to consider though:

    1.) This is a big one; when you first assign a product to a category in any other way, the sort order default is always “1”. Using the API there is no quick, easy, super painless way to change this. Magento is pretty set on having the default as “1”. Depending on how you import your data and how you’re receiving it, you’ll want to take a certain approach to ensure that products that don’t contain this data aren’t at the front.

    2.) If a product is removed from a category, it loses its positioning also, even if you reassign the product back to that category.

Sort Order to Products in a Category

sort order to products

Keeping the data intact for this can be tricky when you’re managing your merchandising fully via the Magento API. The good news is that the actual process and method is very easy to code and implement. There will just be a lot of caveats and gotcha’s that you won’t be expecting. It’s not impossible, and very doable. It does require good planning, communication, and teamwork between yourself and the client. What to keep in mind when it comes to eCommerce merchandising, from a strategy standpoint, is providing customers the option to build their journey without overwhelming them. Confused customers leave websites! We don’t want that, of course. What I’m here to help you with in this quick Magento tutorial is take this strategy and programmatically build a way to assist with way-finding, so that your products can get sorted according to an assigned order within a category. Evaluate your methods then implement to excel your eCommerce merchandising, and in turn the overall success of your online store.

Comment below if you have any questions!

Related: Upserting Product Images via the Magento API