Manish Patel
Manish Patel

Reputation: 89

Sku not working in magento product update API

$result = $client->call($session, 'catalog_product.update', array('123', array(
                                                            'name' => 'Product333222'
                                                        )
                                                    )
                                                    );

Here '123' is the Sku of product. Sku is not working here in update Api.

If i give Product ID in place of Sku it is working fine.

So what is the Issue behind that.

If anyone Knows please let me know.

Thanks.

Upvotes: 1

Views: 2773

Answers (1)

Robin31
Robin31

Reputation: 86

Magento is a bit dull here.

Long story short:

If you are using a numeric value without specifying an identification type its assuming you are doing your works on a product id. If you where to insert "abc" as a value (not numeric) it will be treated as if it were a SKU.

Best way to solve this is to use an identification type (in your case "SKU") in your api call.

Please see this for more info on using the identification type. http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.update.html

Or see: Magento 1.5, numeric SKUs and productIdentifierType

Short story long:

The following function gets called trough the api app/code/core/Mage/Catalog/Model/Api/Resource.php

protected function _getProduct($productId, $store = null, $identifierType = null)
{
    $product = Mage::helper('catalog/product')->getProduct($productId, $this->_getStoreId($store), $identifierType);
    if (is_null($product->getId())) {
        $this->_fault('product_not_exists');
    }
    return $product;
}

As you can see that function is calling the following function in the product helper:

public function getProduct($productId, $store, $identifierType = null) {
        $loadByIdOnFalse = false;
        if ($identifierType == null) {
            if (is_string($productId) && !preg_match("/^[+-]?[1-9][0-9]*$|^0$/", $productId)) {
                $identifierType = 'sku';
                $loadByIdOnFalse = true;
            } else {
                $identifierType = 'id';
            }
        }

        /** @var $product Mage_Catalog_Model_Product */
        $product = Mage::getModel('catalog/product');
        if ($store !== null) {
            $product->setStoreId($store);
        }
        if ($identifierType == 'sku') {
            $idBySku = $product->getIdBySku($productId);
            if ($idBySku) {
                $productId = $idBySku;
            }
            if ($loadByIdOnFalse) {
                $identifierType = 'id';
            }
        }

        if ($identifierType == 'id' && is_numeric($productId)) {
            $productId = !is_float($productId) ? (int) $productId : 0;
            $product->load($productId);
        }

        return $product;
    }

Without specifying an $identifierType here and using a sku like '123' the thrid line is going to do a preg match with will result in true. Thus using its else function threating it as an ID in stead of sku.

In the end:

So, do your call like:

$result = $client->call($session, 'catalog_product.update', array('123', array(
                                                            'name' => 'Product333222'
                                                        ), null, 'sku'));

Upvotes: 3

Related Questions