Commit 3182a8bb by halweg

feat : order rest api 添加商图片url

parent 6d95f0b6
<?php
namespace Joshine\Sales\Plugin;
use Magento\Catalog\Model\ProductFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;
use Magento\Catalog\Helper\Image;
/**
* Class OrderItemRepositoryPlugin
*/
class OrderRepositoryPlugin
{
/**
* Order feedback field name
*/
const FIELD_NAME = 'image_url';
/**
* @var OrderItemExtensionFactory
*/
private $orderItemExtensionFactory;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var \Magento\Sales\Api\ProductRepositoryInterfaceFactory
*/
private $productRepository;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
private $_productRepository;
/**
* @var Image
*/
private $imageHelper;
/**
* @var ProductFactory
*/
private $productFactory;
public function __construct(
OrderRepositoryInterface $orderRepository,
\Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Helper\Image $imageHelper,
ProductFactory $productFactory
)
{
$this->orderItemExtensionFactory = $orderItemExtensionFactory;
$this->orderRepository = $orderRepository;
$this->_productRepository = $productRepository;
$this->imageHelper = $imageHelper;
$this->productFactory = $productFactory;
}
public function afterGet(OrderRepositoryInterface $subject, OrderInterface $quote)
{
return $this->setAttributeValue($quote);
}
private function setAttributeValue($quote) {
foreach ($quote->getItems() as $item) {
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->orderItemExtensionFactory->create();
}
$extensionAttributes->setImage("null");
$product = $this->_productRepository->get($item->getSku());
if ($product) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$hostname = $storeManager->getStore()->getBaseUrl();
$extensionAttributes->setImage($hostname."media/catalog/product/".$product->getData('thumbnail'));
}
}
return $quote;
}
}
\ No newline at end of file
...@@ -6,4 +6,7 @@ ...@@ -6,4 +6,7 @@
<type name="\Magento\Sales\Block\Adminhtml\Order\View\Items"> <type name="\Magento\Sales\Block\Adminhtml\Order\View\Items">
<plugin name="order-item-sort" type="Joshine\Sales\Plugin\ItemsSortPlugin" sortOrder="1" /> <plugin name="order-item-sort" type="Joshine\Sales\Plugin\ItemsSortPlugin" sortOrder="1" />
</type> </type>
<type name="\Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="add-items-image" type="Joshine\Sales\Plugin\OrderRepositoryPlugin" />
</type>
</config> </config>
\ No newline at end of file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Sales\Api\Data\OrderItemInterface">
<attribute code="image" type="string" />
</extension_attributes>
</config>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment