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

时间:2016-06-10 09:37:20

标签: api rest product magento2

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

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

中使用该键
  

POST / V1 / products

&安培;

  

PUT / V1 / products / {sku}

参数尝试逐个参数

  • STORE_ID = 0
  • STOREID = 0 使用以下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"
    }
}

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

GET / V1 / products 有参数storeId 同样我试过PUT& POST但不使用PUT& POST

3 个答案:

答案 0 :(得分:7)

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

<form method="post"

这很好。

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

答案 1 :(得分:5)

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

这个可行,你可以使用

  • 所有
  • 默认
商店代码

答案 2 :(得分:4)

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

我已经超越 Magento \ Store \ Model \ StoreManager ,如下所示:

  

等/ 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参数 storeId 存在而不是存储其他商店调用 parent :: getStore()

并在 REST API PUT 呼叫中,我在所有请求中添加了 storeId ,我需要根据该请求设置要存储的信息StoreID&amp;它像一个魅力:) 对于管理员中的商店值,我使用 storeID = 0 ByDefault获取所有PUT请求。