Magento2:REST API:按商店视图保存产品详细信息不起作用 [英] Magento2: REST API : Save Product Detail per store view not working

查看:30
本文介绍了Magento2:REST API:按商店视图保存产品详细信息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Magento2.1.0-rc1 分支使用样本数据

使用 REST API catalogProductRepositoryV1 REF:http://devdocs.magento.com/swagger/index.html从管理令牌 API 获取密钥并使用该键

<块引用>

POST/V1/products

&

<块引用>

PUT/V1/products/{sku}

with parameter 一一尝试了两个参数

  • store_id=0
  • storeId=0使用以下 JSON

{"saveOptions": "true",产品": {"name": "Test11_11","sku": "TESTOPP_111","attributeSetId": "15","价格": "10",重量":10",状态":1","可见性": "3",自定义属性":[{"attributeCode": "制造商",值":222"},{"attributeCode": "tax_class_id",值":0"},{"attributeCode": "特价",价值":10"},{"attributeCode": "描述",价值":44332211"},{"attributeCode": "eco_collection",值":1"}],"typeId": "简单"}}

不支持 store_id/storeId 字段,但产品中的信息不会保存到商店它保存到默认商店 ID

GET/V1/products 有参数 storeId我曾尝试过 PUT &POST 但不使用 PUT &发布

解决方案

在 Magento2 上调试了很多之后,发现 Magento2 没有任何功能可以根据 StoreID 从 REST API 存储数据StoreManager 中的 getStore 函数只检查会话中是否存在商店,否则返回默认值,这就是为什么所有 REST API 调用都存储在默认商店 ID 中的原因

我已经超越了 Magento\Store\Model\StoreManager 如下:

<块引用>

etc/di.xml

<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 模型/EmizenStoreManager.php

storeRepository = $storeRepository;$this->websiteRepository = $websiteRepository;$this->groupRepository = $groupRepository;$this->scopeConfig = $scopeConfig;$this->storeResolver = $storeResolver;$this->cache = $cache;$this->_request = $request;$this->isSingleStoreAllowed = $isSingleStoreAllowed;}/*** {@inheritdoc}*/公共函数 getStore($storeId = null){if($this->_request->isPut() && strlen($this->_request->getParam('storeId'))){return parent::getStore($this->_request->getParam('storeId'));}return parent::getStore($storeId);}}

在这个文件中,我检查了请求类型是否为PUT和 URL Paramater storeId 存在于 Set that Store else call parent::getStore()

REST API PUT 调用中,我在所有需要设置要存储的信息的请求中添加了 storeIdStoreID &它就像一个魅力:)对于 admin 中的存储值,我对所有 PUT 请求使用 storeID=0 ByDefault.

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

解决方案

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.

这篇关于Magento2:REST API:按商店视图保存产品详细信息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆