我们使用Magento的2.0。我挣扎于购物车列表页面,以显示产品图片。
有了这个:
V1/carts/mine/items
(车上市API)我无法得到的产品图片。
所以我使用的V1/products/(sku)/media
API来获取产品图片通过调用它在一个与产品SKU / s的从车列表API得到循环,以显示它在列表中。
我觉得这是不公平的。因为如果我有10种产品在购物车,然后我需要调用API V1/products/(sku)/media
10倍,这让我的应用程序缓慢,也让我的用户厌倦了等待。当然,我可以加载产品图像异步但甚至10产品图像API调用+ 1个列出购物API + 1个总计购物API(carts/mine/totals
)=每列表12层的API。如果有任何修改或删除发生这应该再次发生。
推荐一种更好的方式,或者是有任何URL或过滤选项来获得所有车产品图像在一个API?
我在此为粘贴该问题的模块。
创建REST API模块
按照以下步骤通过REST API来获取产品缩略图中车无张贴任何值。这将需要一个产品的当前缩略图。 REST URL:
方法:GET - >休息/ V1 /客推车/ cartId /项目创建一个模块:代码/ VENDOR_NAME / MODULE_NAME /
为registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
创建module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Zyxware. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="VendorName_ModuleName" setup_version="1.0.0" />
</config>
创建等/ extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Zyxware, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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>
创建等/ events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Zyxware, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="vendorname_modulename_sales_quote_load_after" instance="VendorNmae\ModuleName\Observer\ProductInterface" />
</event>
</config>
创建观察报:VENDOR_NAME / Mocule_name /观察员/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Zyxware, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorName\ModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Api\ProductRepositoryInterfaceFactory as ProductRepository;
use Magento\Catalog\Helper\ImageFactory as ProductImageHelper;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use Magento\Store\Model\App\Emulation as AppEmulation;
use Magento\Quote\Api\Data\CartItemExtensionFactory;
class ProductInterface implements ObserverInterface
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var \Magento\Catalog\Helper\ImageFactory
*/
protected $productImageHelper;
/**
*@var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
*@var \Magento\Store\Model\App\Emulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $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();
/**
* Code to add the items attribute to extension_attributes
*/
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;
}
/**
* Helper function that provides full cache image 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;
}
}
JSON输出:
[
{
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes": {
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
}
}
]
之前检查你出来说,如果你安装了正确的方法,你可以检查你的VAR /生成/ Magento的/报价/原料药/数据/ CartItemExtension.php具有这样的价值:
<?php
namespace Magento\Quote\Api\Data;
/**
* Extension class for @see \Magento\Quote\Api\Data\CartItemInterface
*/
class CartItemExtension extends \Magento\Framework\Api\AbstractSimpleObject implements \Magento\Quote\Api\Data\CartItemExtensionInterface
{
/**
* @return string|null
*/
public function getImageUrl()
{
return $this->_get('image_url');
}
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
{
$this->setData('image_url', $imageUrl);
return $this;
}
}