代码之家  ›  专栏  ›  技术社区  ›  Abirami Bala

从购物车列表的magento 2.0 REST Api获取多个产品图像-Swift 4

  •  1
  • Abirami Bala  · 技术社区  · 7 年前

    我们正在使用 Magento 2.0版 . 我在购物车列表页面挣扎着显示产品图片。

    有了这个:

    V1/carts/mine/items (购物车列表api)我无法获取产品图像。

    所以我用的是 V1/products/(sku)/media api,通过使用从cart list api获得的产品sku/s在for循环中调用它,使产品图像显示在列表中。

    我认为这不公平。因为如果我的购物车里有10种产品,我需要打电话给 V1/产品/(sku)/媒体 api 10次,这使得我的应用程序运行缓慢,也让我的用户厌倦了等待。当然,我可以异步加载产品映像,但即使是10个产品映像api调用+1个购物车列表api+1个购物车总计api( carts/mine/totals )=每个清单12个api。如果发生任何编辑或删除操作,则应再次执行此操作。

    建议一种更好的方法,或者是否有任何url或过滤选项可以在一个api中获取所有购物车产品图像?

    1 回复  |  直到 7 年前
        1
  •  2
  •   N.ameen    7 年前

    我在此粘贴这个问题的模块。

    为rest api创建模块

    按照以下步骤通过Rest API在Cart中获取产品缩略图,而不发布任何值。它将获取产品的当前缩略图图像。休息Url:

    方法:GET->rest/V1/guest carts/cartId/items 创建模块:代码/供应商名称/模块名称/

    注册.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>
    

    创建etc/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>
    

    创建etc/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>
    

    创建观察者:供应商名称/Mocule名称/Observer/

    产品接口.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/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;
        }
    }
    
    推荐文章