Emi*_*ech 8 api rest product magento2
使用Magento2.1.0-rc1分支与样本数据
使用REST API catalogProductRepositoryV1 REF:http://devdocs.magento.com/swagger/index.html 从管理令牌API获取密钥并使用该密钥
POST/V1 /产品
&
PUT/V1/products/{sku}
with参数尝试逐个参数
{
"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"
}
}Run Code Online (Sandbox Code Playgroud)
不支持store_id/storeId字段,但产品中的信息不保存以存储它保存到默认的Store ID
GET/V1/products有参数storeId和PUT&POST一样,但没有使用PUT&POST
我遇到过类似的情况,我想更新每个网站的价格.所以为了更新价格,我已经习惯了
/rest/<store_code>/V1/products/<sku>
Run Code Online (Sandbox Code Playgroud)
这很好.
所以我假设您可以使用它来更新每个商店的产品数据.
在 Magento2 上进行大量调试后,发现 Magento2 没有任何从 REST API 存储数据的功能,按照 StoreManager中的 StoreID 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>
Run Code Online (Sandbox Code Playgroud)
vim 模型/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);
}
}
Run Code Online (Sandbox Code Playgroud)
在此文件中,我检查请求类型是否为PUT 并且 URL 参数storeId是否存在而不是设置该存储,否则调用parent::getStore()
在REST API PUT调用中,我在所有请求中添加了storeId,其中我需要设置要根据 StoreID 存储的信息,它的工作原理就像一个魅力:) 对于管理中的存储值,我对所有 PUT 使用storeID=0 ByDefault要求。
| 归档时间: |
|
| 查看次数: |
6131 次 |
| 最近记录: |