Skip to content

Commit

Permalink
Draft for preventing product load in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Nov 9, 2023
1 parent cbf4a37 commit f89fc8b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 9 deletions.
9 changes: 5 additions & 4 deletions DataLayer/Mapper/CartItemDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Tax\Model\Config;
use Yireo\GoogleTagManager2\Util\PriceFormatter;
use Yireo\GoogleTagManager2\Util\ProductProvider;

class CartItemDataMapper
{
private ProductDataMapper $productDataMapper;
private ProductRepositoryInterface $productRepository;
private ProductProvider $productProvider;
private PriceFormatter $priceFormatter;
private ScopeConfigInterface $scopeConfig;

Expand All @@ -25,12 +26,12 @@ class CartItemDataMapper
*/
public function __construct(

Check failure on line 27 in DataLayer/Mapper/CartItemDataMapper.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @param references unknown parameter: $productRepository
ProductDataMapper $productDataMapper,
ProductRepositoryInterface $productRepository,
ProductProvider $productProvider,
PriceFormatter $priceFormatter,
ScopeConfigInterface $scopeConfig
) {
$this->productDataMapper = $productDataMapper;
$this->productRepository = $productRepository;
$this->productProvider = $productProvider;
$this->priceFormatter = $priceFormatter;
$this->scopeConfig = $scopeConfig;
}
Expand All @@ -43,7 +44,7 @@ public function __construct(
public function mapByCartItem(CartItemInterface $cartItem): array
{
try {
$product = $this->productRepository->get($cartItem->getSku());
$product = $this->productProvider->getBySku($cartItem->getSku());
$cartItemData = $this->productDataMapper->mapByProduct($product);
} catch (NoSuchEntityException $e) {
$cartItemData = [];
Expand Down
8 changes: 4 additions & 4 deletions DataLayer/Mapper/ProductDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ProductDataMapper
private PriceFormatter $priceFormatter;

private array $dataLayerMapping;

private int $counter = 0;

/**
Expand All @@ -47,12 +47,12 @@ public function __construct(
}

/**
* @param Product $product
* @param ProductInterface $product
* @return array
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function mapByProduct(Product $product): array
public function mapByProduct(ProductInterface $product): array
{
$prefix = 'item_';
$productData = [];
Expand Down Expand Up @@ -84,7 +84,7 @@ public function mapByProduct(Product $product): array
$productData = $this->attachCategoriesData($product, $productData);
$productData = $this->parseDataLayerMapping($product, $productData);
$productData['index'] = $this->counter++;

// @todo: Add "variant" reference to Configurable Product

return $productData;
Expand Down
23 changes: 22 additions & 1 deletion DataLayer/Tag/Cart/CartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Magento\Checkout\Model\Cart as CartModel;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote\Item;
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
use Yireo\GoogleTagManager2\DataLayer\Mapper\CartItemDataMapper;
use Yireo\GoogleTagManager2\Util\ProductProvider;

class CartItems implements TagInterface
{
private CartModel $cartModel;
private CartItemDataMapper $cartItemDataMapper;
private ProductProvider $productProvider;

/**
* @param CartModel $cartModel
Expand All @@ -21,10 +24,12 @@ class CartItems implements TagInterface
*/
public function __construct(
CartModel $cartModel,
CartItemDataMapper $cartItemDataMapper
CartItemDataMapper $cartItemDataMapper,
ProductProvider $productProvider
) {
$this->cartModel = $cartModel;
$this->cartItemDataMapper = $cartItemDataMapper;
$this->productProvider = $productProvider;
}

/**
Expand All @@ -38,12 +43,28 @@ public function get(): array
return [];
}

$this->productProvider->setProductSkus($this->getSkusFromCartItems($cartItems));
$cartItemsData = [];

foreach ($cartItems as $cartItem) {
$cartItemData = $this->cartItemDataMapper->mapByCartItem($cartItem);
$cartItemsData[] = $cartItemData;
}

return $cartItemsData;
}

/**
* @param Item[] $cartItems
* @return array
*/
private function getSkusFromCartItems(array $cartItems): array
{
$productSkus = [];
foreach ($cartItems as $cartItem) {
$productSkus[] = $cartItem->getSku();
}

return $productSkus;
}
}
80 changes: 80 additions & 0 deletions Util/ProductProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);

namespace Yireo\GoogleTagManager2\Util;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use RuntimeException;

class ProductProvider
{
private ProductRepositoryInterface $productRepository;
private SearchCriteriaBuilder $searchCriteriaBuilder;

/**
* @var string[]
*/
private array $productSkus = [];

public function __construct(
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @param string[] $productSkus
* @return void
*/
public function setProductSkus(array $productSkus)
{
$this->productSkus = $productSkus;
}

/**
* @return ProductInterface[]
*/
public function getProducts(): array
{
static $products = null;
if (!empty($products)) {
return $products;
}

if (empty($this->productSkus)) {
throw new RuntimeException('Using getProducts() before setProductSkus()');
}

$this->searchCriteriaBuilder->addFilter('sku', $this->productSkus, 'IN');
$searchCriteria = $this->searchCriteriaBuilder->create();
$products = $this->productRepository->getList($searchCriteria)->getItems();

return $products;
}

/**
* @param string $sku
* @return ProductInterface
* @throws NoSuchEntityException
*/
public function getBySku(string $sku): ProductInterface
{
foreach ($this->getProducts() as $product) {
if ($product->getSku() === $sku) {
return $product;
}
}

throw new NoSuchEntityException(__('No product with sku "'.$sku.'"'));
}


public function reset()
{
$this->productSkus = [];
}
}

0 comments on commit f89fc8b

Please sign in to comment.