[Mini-Tutorial] Creating and Assigning Products to Categories

Categories are one of the most important aspects of your website! A healthy, clean category structure can direct your customers to exactly what they are looking for in only a few clicks. A messy, bloated structure can leave your customers confused, and frustrated on how your site is laid out. If customers can’t find the products on your website, then there’s no point in having an online store – okay, I’m being a little dramatic. But I want to make it clear that the product finding experience is crucial to your store, so you need to think about how users are navigating your site. In Magento, product categories are a great way to sort and organize your products.

What I’m not going to do today is tell you how to organize your categories. That’s a job better suited for Demac Media’s amazing Commerce Strategy team. I’m here to tell you how to create, and assign products to them via the API. So let’s get going!

Related: How to Set Up Your Magento Store

Creating and Assigning Products to Categories

magento

The first thing you’ll always need to do is get the current category tree in Magento.

service.catalogCategoryTree(string sessionId, string parentId, string storeview);

  • sessionId – The id obtained after logging into the API.
  • parentId – The root category you want to query for. Typically you’ll want the full catalog tree (there may be times where you only want to look at a subset of
  • categories – so Magento API gives you the flexibility to return only those results). Using Magento pre-created root, you’ll be using the ID ‘2’ for Enterprise installs.
  • Storeview – Which storeview you want the results from. (Useful if you want category names in a different language, or you have multiple sites and need a specific category tree)

What Your Results will Look Like

So you’ve made your call to the API, you’ve got your results, here’s a visual of what the results look like compared to what is visible in the backend.

cattree

The short of it is, the results mimic the parent <-> child relationship of the category tree to a T. If you want to drill down to a very specific subcategory, you’ll have to navigate your category tree object the same way. Recursive methods, although dangerous, are a very viable option of effectively navigating these results.

So now you’ve got a new category to add.

Related: [Mini-Tutorial] Magento + Fishpig: Give Your eCommerce Blog a Face

How do you add it?

int service.catalogCategoryCreate(string sessionId, int parentId, catalogCategoryEntityCreate categoryData, string storeView);

parentId, in this case, is the direct parent to the current category you create, it is not necessarily your root Id. Example: If you wanted to create “Shirts” under “Boys”. Assuming “Boys” CategoryId is 15, parentId would be 15 in this case.

There’s this other object we need to take a look at – catalogCategoryEntityCreate

name – The name customers will see on the frontend
is_active – 0 or 1. Is this category visible on the frontend?
is_activeSpecified – I always set this to true.
is_anchor – 0 or 1. Is this category an anchor?
is_anchorSpecified – I always set this to true.
include_in_menu – 0 or 1. Sets “Include in Navigation Menu”
include_in_menuSpecified – I always set this to true.
available_sort_by – array of string. This is a required field. Which attributes do you want a customer to be allowed to sort by when browsing in this category? Use attribute codes for each element of your array.
default_sort_by – string. When a customer first lands on this category page, what attribute are the products sorted by?

The integer returned upon creation is the categoryId of your new category.

You may have noticed a slight hole of information missing from this. What if I want to set the name of the category for other languages? Well no problem! Luckily you just retrieved the categoryId from the API so it’s as simple as.

service.catalogCategoryUpdate(string sessionId, int categoryId, catalogCategoryEntityCreate categoryData, string storeview)

I always pass in all the same information as the create. Except of course using the actual categoryId you’re updating instead of the parentId. Throw in the storeview for the language you’d like to update, and make sure the categoryData.name is the correct language. Now you’ve got your fancy category tree, it looks real nice, has your multi-lingual support. All that’s left is to put products in there!

Related: [Mini Tutorial] Increase Conversion with these Magento Checkout Layout Changes

Combine this with your standard product import/update.

In object catalogProductCreateEntity (use this object for updates also):

string[] category_ids

Just pass in the array of category_ids this product should belong to. Programmatically, you can obtain these ids by recursively looping through your category tree you retrieved from service.catalogCategoryTree. Call service.catalogProductUpdate or service.catalogProductCreate to create/update your product and populate those categories!

Want more helpful Magento resources? Start with our free eBook including 25 Examples of Magento Shopping Cart Price rules





Download All 25 Examples!