ImageProvider.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model\Cart;
  7. use Magento\Checkout\CustomerData\DefaultItem;
  8. use Magento\Framework\App\ObjectManager;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class ImageProvider
  14. {
  15. /**
  16. * @var \Magento\Quote\Api\CartItemRepositoryInterface
  17. */
  18. protected $itemRepository;
  19. /**
  20. * @var \Magento\Checkout\CustomerData\ItemPoolInterface
  21. * @deprecated 100.2.7 No need for the pool as images are resolved in the default item implementation
  22. * @see \Magento\Checkout\CustomerData\DefaultItem::getProductForThumbnail
  23. */
  24. protected $itemPool;
  25. /**
  26. * @var \Magento\Checkout\CustomerData\DefaultItem
  27. * @since 100.2.7
  28. */
  29. protected $customerDataItem;
  30. /**
  31. * @param \Magento\Quote\Api\CartItemRepositoryInterface $itemRepository
  32. * @param \Magento\Checkout\CustomerData\ItemPoolInterface $itemPool
  33. * @param DefaultItem|null $customerDataItem
  34. */
  35. public function __construct(
  36. \Magento\Quote\Api\CartItemRepositoryInterface $itemRepository,
  37. \Magento\Checkout\CustomerData\ItemPoolInterface $itemPool,
  38. \Magento\Checkout\CustomerData\DefaultItem $customerDataItem = null
  39. ) {
  40. $this->itemRepository = $itemRepository;
  41. $this->itemPool = $itemPool;
  42. $this->customerDataItem = $customerDataItem ?: ObjectManager::getInstance()->get(DefaultItem::class);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getImages($cartId)
  48. {
  49. $itemData = [];
  50. /** @see code/Magento/Catalog/Helper/Product.php */
  51. $items = $this->itemRepository->getList($cartId);
  52. /** @var \Magento\Quote\Model\Quote\Item $cartItem */
  53. foreach ($items as $cartItem) {
  54. $allData = $this->customerDataItem->getItemData($cartItem);
  55. $itemData[$cartItem->getItemId()] = $allData['product_image'];
  56. }
  57. return $itemData;
  58. }
  59. }