|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Stape\Gtm\ViewModel\Wishlist; |
| 4 | + |
| 5 | +use Magento\Catalog\Block\Product\Context; |
| 6 | +use Magento\Framework\Serialize\Serializer\Json; |
| 7 | +use Magento\Framework\View\Element\Block\ArgumentInterface; |
| 8 | +use Magento\Framework\View\Layout; |
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
| 10 | +use Magento\Wishlist\Block\Customer\Wishlist; |
| 11 | +use Stape\Gtm\Model\Product\Mapper\EventItemsMapper; |
| 12 | +use Stape\Gtm\ViewModel\DatalayerInterface; |
| 13 | + |
| 14 | +class ExtraData implements ArgumentInterface, DatalayerInterface |
| 15 | +{ |
| 16 | + |
| 17 | + /** |
| 18 | + * @var Layout $layout |
| 19 | + */ |
| 20 | + protected $layout; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var StoreManagerInterface $storeManager |
| 24 | + */ |
| 25 | + protected $storeManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var EventItemsMapper $mapper |
| 29 | + */ |
| 30 | + protected $mapper; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Json $json |
| 34 | + */ |
| 35 | + protected $json; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Magento\Wishlist\Helper\Data $wishlistHelper |
| 39 | + */ |
| 40 | + protected $wishlistHelper; |
| 41 | + |
| 42 | + /** |
| 43 | + * Define class dependencies |
| 44 | + * |
| 45 | + * @param Layout $layout |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + Layout $layout, |
| 49 | + StoreManagerInterface $storeManager, |
| 50 | + EventItemsMapper $mapper, |
| 51 | + Json $json, |
| 52 | + Context $context |
| 53 | + ) { |
| 54 | + $this->layout = $layout; |
| 55 | + $this->storeManager = $storeManager; |
| 56 | + $this->mapper = $mapper; |
| 57 | + $this->json = $json; |
| 58 | + $this->wishlistHelper = $context->getWishlistHelper(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Retrieve json |
| 63 | + * |
| 64 | + * @return bool|string |
| 65 | + */ |
| 66 | + public function getJson() |
| 67 | + { |
| 68 | + $wishlist = $this->wishlistHelper->getWishlist(); |
| 69 | + $items = array_map(function($item) { |
| 70 | + return $item->getProduct(); |
| 71 | + }, $wishlist->getItemCollection()->getItems()); |
| 72 | + |
| 73 | + return $this->json->serialize([ |
| 74 | + 'currency' => $this->storeManager->getStore()->getCurrentCurrency()->getCode(), |
| 75 | + 'lists' => [ |
| 76 | + [ |
| 77 | + 'item_list_name' => 'products', |
| 78 | + 'items' => $this->mapper->toEventItems($items) |
| 79 | + ] |
| 80 | + ] |
| 81 | + ]); |
| 82 | + } |
| 83 | +} |
0 commit comments