cancel
Showing results for 
Search instead for 
Did you mean: 

Magento API stockItems

Magento API stockItems

Hi I need to call stockItems/{sku} to get the stock for a product, but the sku containtsa special characters like / . I have tried to encode but I don t know how magento is handling this . Please help 

1 REPLY 1

Re: Magento API stockItems

Hello @lavinia_balan 

 

You can try with Plugin.

Get and put product, you can plugged in before the get and save function in \Magento\Catalog\Api\ProductRepositoryInterface.

 

<?phpnamespace Vendor\Module\Plugin\Product;

class RestApiSkuFix
{
    /**
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     * @param string $sku
     * @param bool $editMode
     * @param int|null $storeId
     * @param bool $forceReload
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @return array
     */
    public function beforeGet($productRepository, $sku, $editMode = false, $storeId = null, $forceReload = false)
    {        $sku = urldecode($sku);
        return [$sku, $editMode, $storeId, $forceReload];
    }

    /**
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     * @param \Magento\Catalog\Api\Data\ProductInterface $product
     * @param bool $saveOptions
     * @return array
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\StateException
     * @throws \Magento\Framework\Exception\CouldNotSaveException
     */
    public function beforeSave($productRepository, \Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
    {        $product->setSku(urldecode($product->getSku()));
    }
}

And this I did only in rest webapi by registering the plugin in Vendor/Module/etc/webapi_rest/di.xml

 

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\ProductRepository">
        <plugin name="ProductRepositoryRestApiSkuFix" type="\Vendor\Module\Plugin\Product\RestApiSkuFix" />
    </type>
</config>

 

Then you can get and put product with slash by

get /rest/V1/products/100%252f1

and

put /rest/V1/products/100%252f1
If my answer is useful, please Accept as Solution & give Kudos