Magento2:REST API:按商店视图保存产品详细信息不起作用

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

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

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

中使用该键

POST /V1/products

&

PUT /V1/products/{sku}

带参数一个一个地尝试两个参数

{
    "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"
    }
}

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

GET /V1/products 有参数storeId 同样我曾尝试使用 PUT & POST 但没有使用 PUT & POST

在 Magento2 上进行了大量调试后,发现 Magento2 没有任何功能可以根据 StoreID 从 REST API 存储数据 getStore StoreManager 中的函数只是检查会话中是否存在商店 else return default ,这就是为什么所有 REST API 调用存储在默认商店 ID

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

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);
    }

}

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

并且在RESTAPIPUT调用中,我添加了storeId在我需要将信息设置为按照 StoreID 存储的所有请求中,它就像一个魅力:) 对于管理员中的存储值,我对所有 PUT 请求使用 storeID=0 ByDefault。

我遇到过类似的情况,我想更新每个网站的价格。所以为了更新价格,我使用了

/rest/<store_code>/V1/products/<sku>

这很好用。

所以我假设您可以使用它来更新每个商店的产品数据。

/rest/<store_code>/V1/products/<sku>

这个有效,你可以用

  • 全部
  • 默认

商店代码