Emizen Tech Emizen Tech - 1 month ago 15
reST (reStructuredText) Question

Magento2: REST API : Save Product Detail per store view not working

Using Magento2.1.0-rc1 Branch
With Sample Data

Using REST API catalogProductRepositoryV1 REF: http://devdocs.magento.com/swagger/index.html
Get Key from Admin token API
and use that key in


POST /V1/products


&


PUT /V1/products/{sku}


with parameter tried with both parameter one by one


  • store_id=0

  • storeId=0
    using following JSON





{
"saveOptions": "true",
"product": {
"name": "Test11_11",
"sku": "TESTOPP_111",
"attributeSetId": "15",
"price": "10",
"weight": "10",
"status": "1",
"visibility": "3",
"customAttributes": [
{
"attributeCode": "manufacturer",
"value": "222"
},
{
"attributeCode": "tax_class_id",
"value": "0"
},
{
"attributeCode": "specialPrice",
"value": "10"
},
{
"attributeCode": "description",
"value": "44332211"
},
{
"attributeCode": "eco_collection",
"value": "1"
}
],
"typeId": "simple"
}
}





Does not support store_id / storeId field ,
but the information in product does not save to store
it save to default Store ID

GET /V1/products has parameter storeId
same i had tried with PUT & POST but not working with PUT & POST

Answer

after debugging a lot on Magento2, Found that Magento2 does not have any functionality to Store Data from REST API as per StoreID getStore function in StoreManager just check if store is exist in session else return default , that is why all REST API Calls are stored in default store ID

I have Over Rided Magento\Store\Model\StoreManager as below :

etc/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">
    <preference for="Magento\Store\Model\StoreManager" type="Emizentech\MobileAdmin\Model\EmizenStoreManager" />
</config>

vim Model/EmizenStoreManager.php

<?php
namespace Emizentech\MobileAdmin\Model;

use Magento\Store\Api\StoreResolverInterface;
use Magento\Framework\App\RequestInterface;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class EmizenStoreManager extends \Magento\Store\Model\StoreManager
{
        /**
     * Request instance
     *
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $_request;

         /**
     * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
     * @param \Magento\Store\Api\GroupRepositoryInterface $groupRepository
     * @param \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param StoreResolverInterface $storeResolver
     * @param \Magento\Framework\Cache\FrontendInterface $cache
     * @param bool $isSingleStoreAllowed
     */
    public function __construct(
        \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
        \Magento\Store\Api\GroupRepositoryInterface $groupRepository,
        \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        StoreResolverInterface $storeResolver,
        \Magento\Framework\Cache\FrontendInterface $cache,
        RequestInterface $request,
        $isSingleStoreAllowed = true
    ) {
        $this->storeRepository = $storeRepository;
        $this->websiteRepository = $websiteRepository;
        $this->groupRepository = $groupRepository;
        $this->scopeConfig = $scopeConfig;
        $this->storeResolver = $storeResolver;
        $this->cache = $cache;
        $this->_request = $request;
        $this->isSingleStoreAllowed = $isSingleStoreAllowed;
    }
    /**
     * {@inheritdoc}
     */
    public function getStore($storeId = null)
    {

                if($this->_request->isPut() && strlen($this->_request->getParam('storeId')))
                {
                        return parent::getStore($this->_request->getParam('storeId'));
                }
                return parent::getStore($storeId);
    }

}

in this file i have check that if Request type is PUT and URL Paramater storeId exist than Set that Store else call parent::getStore()

and in REST API PUT Call, I have added storeId in all request in which I need to set information to be stored as per StoreID & it works like a charm :) for store values in admin i am using storeID=0 ByDefault for all PUT Requests.

Comments