浏览文章
文章信息
Magento2 rest api 扩展属性|例如在返回的购物车接口中添加item产品图片
11875
1、为Magento\Quote\Api\Data\CartItemInterface扩展属性
etc/extension_attributes.xml
内容:
<?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\Quote\Api\Data\CartItemInterface"> <attribute code="image_url" type="string" /> </extension_attributes> </config>2、声明观察者修改读取item后添加item产品图
etc/events.xml
内容:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="sales_quote_load_after"> <observer name="Aiweline_SimplePageShop_sales_quote_load_after" instance="Aiweline\SimplePageShop\Observer\CartItemAddImage" /> </event> </config>3、创建观察者CartItemAddImage
Aiweline\SimplePageShop\Observer\CartItemAddImage
内容:
<?php /** * @Author 秋枫雁飞 * @Email aiweline@qq.com/1714255949@qq.com * @Desc 文件由Aiweline(秋枫雁飞)编写,若有升级需要 * 建议不要随意修改文件源码。 **/ namespace Aiweline\SimplePageShop\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Catalog\Api\ProductRepositoryInterfaceFactory as ProductRepository; use Magento\Catalog\Helper\ImageFactory as ProductImageHelper; use Magento\Framework\ObjectManagerInterface; use Magento\Store\Model\StoreManagerInterface as StoreManager; use Magento\Store\Model\App\Emulation as AppEmulation; use Magento\Quote\Api\Data\CartItemExtensionFactory; class CartItemAddImage implements ObserverInterface { /** * @var ObjectManagerInterface */ protected ObjectManagerInterface $_objectManager; /** * @var ProductRepository */ protected ProductRepository $productRepository; /** * @var \Magento\Catalog\Helper\ImageFactory */ protected ProductImageHelper $productImageHelper; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected StoreManager $storeManager; /** * @var \Magento\Store\Model\App\Emulation */ protected AppEmulation $appEmulation; /** * @var CartItemExtensionFactory */ protected CartItemExtensionFactory $extensionFactory; /** * @param \Magento\Framework\ObjectManagerInterface $objectManager * @param ProductRepository $productRepository * @param \Magento\Catalog\Helper\ImageFactory * @param \Magento\Store\Model\StoreManagerInterface * @param \Magento\Store\Model\App\Emulation * @param CartItemExtensionFactory $extensionFactory */ public function __construct( \Magento\Framework\ObjectManagerInterface $objectManager, ProductRepository $productRepository, ProductImageHelper $productImageHelper, StoreManager $storeManager, AppEmulation $appEmulation, CartItemExtensionFactory $extensionFactory ) { $this->_objectManager = $objectManager; $this->productRepository = $productRepository; $this->productImageHelper = $productImageHelper; $this->storeManager = $storeManager; $this->appEmulation = $appEmulation; $this->extensionFactory = $extensionFactory; } public function execute(\Magento\Framework\Event\Observer $observer, string $imageType = NULL) { $quote = $observer->getQuote(); /** * 添加items的属性到扩展属性 */ foreach ($quote->getAllItems() as $quoteItem) { $product = $this->productRepository->create()->getById($quoteItem->getProductId()); $itemExtAttr = $quoteItem->getExtensionAttributes(); if ($itemExtAttr === null) { $itemExtAttr = $this->extensionFactory->create(); } $imageurl = $this->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl(); $itemExtAttr->setImageUrl($imageurl); $quoteItem->setExtensionAttributes($itemExtAttr); } return; } /** * 提供完整缓存图像url的帮助函数 * @param \Magento\Catalog\Model\Product * @return string */ protected function getImageUrl($product, string $imageType = NULL) { $storeId = $this->storeManager->getStore()->getId(); $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true); $imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl(); $this->appEmulation->stopEnvironmentEmulation(); return $imageUrl; } }
安装代码:
bin/magento setup:upgrade bin/magento c:c完毕!