我已经实施了一个自定义magento 2插件,以编程方式更改某些产品的特价。这是前端的成功,但仍然在webapi我得到了原始的特价。
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="Vendor\Module\Api\PromotionManagementInterface" type="Vendor\Module\Model\PromotionManagement"/>
<type name="Magento\Catalog\Model\Product">
<plugin name="change_product" type="Vendor\Module\Plugin\Product" sortOrder="1" disabled="false"/>
</type>
</config>
供应商\模块\插件\ Product.php
使用webapi
时未调用afterGetSpecialPrice()
.....
public function afterGetSpecialPrice(\Magento\Catalog\Model\Product $subject, $result)
{
if(isset($this->data)){
return $this->data['special_price']; // $60.00
}
return $result; // $70.00
}
.....
结果在首页:$ 60.00,结果为webapi:$ 70.00(原价特价)
可能是什么原因?
答案 0 :(得分:0)
根据示例代码magento2-samples,必须在插件类中使用afterGetList方法。
<强> di.xml 强>
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
<plugin name="change_special_price" type="Vendor\Module\Plugin\Repository"/>
</type>
<强>供应商\模块\插件\ Repository.php 强>
<?php
namespace Vendor\Module\Plugin;
class Repository
{
public function afterGetList(\Magento\Catalog\Api\ProductRepositoryInterface $subject,\Magento\Framework\Api\SearchResults $searchResult) {
foreach ($searchResult->getItems() as $product) {
$product->setData('special_price',60.0000); //custom special price
}
return $searchResult;
}
}