|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Stape\Gtm\Model\Product\Mapper; |
| 4 | + |
| 5 | +use Magento\Catalog\Block\Product\Context; |
| 6 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 7 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 8 | +use Magento\Store\Model\StoreManagerInterface; |
| 9 | +use Stape\Gtm\Model\Product\CategoryResolver; |
| 10 | + |
| 11 | +class EventItemsMapper |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * @var PriceCurrencyInterface $priceCurrency |
| 16 | + */ |
| 17 | + protected $priceCurrency; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var StoreManagerInterface $storeManager |
| 21 | + */ |
| 22 | + protected $storeManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var CategoryResolver $categoryResolver |
| 26 | + */ |
| 27 | + protected $categoryResolver; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Context $context |
| 31 | + */ |
| 32 | + protected $context; |
| 33 | + |
| 34 | + /** |
| 35 | + * Define class dependencies |
| 36 | + * |
| 37 | + * @param PriceCurrencyInterface $priceCurrency |
| 38 | + * @param StoreManagerInterface $storeManager |
| 39 | + * @param CategoryResolver $categoryResolver |
| 40 | + * @param Context $context |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + PriceCurrencyInterface $priceCurrency, |
| 44 | + StoreManagerInterface $storeManager, |
| 45 | + CategoryResolver $categoryResolver, |
| 46 | + Context $context |
| 47 | + ) { |
| 48 | + $this->priceCurrency = $priceCurrency; |
| 49 | + $this->storeManager = $storeManager; |
| 50 | + $this->categoryResolver = $categoryResolver; |
| 51 | + $this->context = $context; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Map product collection to event items collection |
| 56 | + * |
| 57 | + * @param \Magento\Catalog\Model\Product[]|\Magento\Catalog\Model\ResourceModel\Product\Collection $itemList |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function toEventItems($itemList) |
| 61 | + { |
| 62 | + $items = []; |
| 63 | + $index = 0; |
| 64 | + $imageBuilder = $this->context->getImageBuilder(); |
| 65 | + |
| 66 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 67 | + foreach ($itemList as $product) { |
| 68 | + $category = $this->categoryResolver->resolve($product); |
| 69 | + $items[$product->getId()] = [ |
| 70 | + 'imageUrl' => $imageBuilder->create($product, 'category_page_grid')->getImageUrl(), |
| 71 | + 'item_name' => $product->getName(), |
| 72 | + 'item_id' => $product->getId(), |
| 73 | + 'item_sku' => $product->getSku(), |
| 74 | + 'price' => $this->priceCurrency->round($product->getFinalPrice()), |
| 75 | + 'index' => $index++, |
| 76 | + 'quantity' => '1', |
| 77 | + 'variant_name' => $product->getName(), |
| 78 | + 'item_category' => $category ? $category->getName() : null, |
| 79 | + ]; |
| 80 | + } |
| 81 | + return $items; |
| 82 | + } |
| 83 | +} |
0 commit comments