Magento API and SKUs. Checking and Retrieving Products using the SKU Tag

What is SKU?

The stock keeping unit (aka SKU) is a special alphanumeric identification tag, which is attached to a product. The SKU allows to easily trace your inventory, thus each tag should be unique.

Magento and SKU

Magento heavily relies on each product having a SKU tag. In this respect, it is a mandatory field, which will not allow to insert a new product, unless you fill it in. And SKUs need to be unique with in your inventory list or the product will not pass. In my case I decided to go a step further, and make sure the SKUs I use will be world wide unique, thus using a human readable unique identifier, which is a 20 digit number.

The SKU Problem

In addition to the SKU, Magento upon product creation will assign an autoincrementing product ID, which it uses internally to identify the product. All of the product management API functions accept both product ID and SKU as identifying argument, However, it turned out, that Magento (tested on version 1.9.2) upon seeing a number, will automatically accept it as being a product ID, which cause lots of errors. For instance, you ask for product details using the SKU tag, and the Magento API throws an error saying the product does not exist, even though the product sits happily in the store. Well, how can this be solved then:

Get Product Info Using the SKU

public function productInfoBySku($sku) {
    $filters = [
        ["sku" => $sku]
    ];
    $result = $this->client->call($this->getSessionId(), 'catalog_product.list', $filters);
    return count($result) > 0 ? $result[0] : null;
}

Checking if Product Exists Using the SKU

Here is how to check if a product exists using the SKU:

public function productExists($sku) {
    $filters = [
        ["sku" => $sku]
    ];
    $result = $this->client->call($this->getSessionId(), 'catalog_product.list', $filters);
    return count($result) > 0 ? true : false;
}

 

 

 

 

 

 

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form. Links to this article are welcome

Other Related Articles