gpt4 book ai didi

r - magento 2 休息 api : get cart items with images

转载 作者:行者123 更新时间:2023-12-02 15:15:46 27 4
gpt4 key购买 nike

在 rest api 中,我使用“/rest/V1/guest-carts/e3e1fd447e0e315dd761942cf949ce5d/items”方法获取 magento 购物车商品。它运作良好,结果是

[
{
"item_id": 100,
"sku": "abc-1",
"qty": 1,
"name": "Product one",
"price": 19,
"product_type": "simple",
"quote_id": "e3e1fd447e0e315dd761942cf949ce5d"
},
{
"item_id": 101,
"sku": "abc-2",
"qty": 1,
"name": "Product two",
"price": 54,
"product_type": "simple",
"quote_id": "e3e1fd447e0e315dd761942cf949ce5d"
}
]

现在我想获取列表中每个产品的图像(可能是缩略图)。有什么办法可以达到这个效果吗?

最佳答案

按照步骤通过 Rest API 获取购物车中的产品缩略图图像,无需 POST 任何值。它将获取产品的当前缩略图。休息网址:

方法:GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items

创建模块:code/Vendor_name/Module_name/

注册.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);

创建模块.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. 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="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>

创建 etc/extension_attributes.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, 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>

创建 etc/events.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, 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="hopescode_mobileshop_sales_quote_load_after" instance="Hopescode\Mobileshop\Observer\ProductInterface" />
</event>
</config>

创建观察者:Vendor_name/Mocule_name/Observer/

ProductInterface.php

<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Hopescode\Mobileshop\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;
}

}

输出:

[
{
"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/generation/Magento/Quote/Api/Data/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;
}
}

关于r - magento 2 休息 api : get cart items with images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40720628/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com