-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft for preventing product load in loop
- Loading branch information
1 parent
cbf4a37
commit f89fc8b
Showing
4 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -25,12 +26,12 @@ class CartItemDataMapper | |
*/ | ||
public function __construct( | ||
Check failure on line 27 in DataLayer/Mapper/CartItemDataMapper.php GitHub Actions / PHPStan
|
||
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; | ||
} | ||
|
@@ -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 = []; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} | ||
} |