From a188718612dab88cb973caa5728f7cfeb298a9a1 Mon Sep 17 00:00:00 2001 From: mohitalgolia <101385480+mohitalgolia@users.noreply.github.com> Date: Wed, 6 Dec 2023 13:56:17 +0530 Subject: [PATCH] Cart recommendations issue for grouped products in add to cart --- ViewModel/Recommend/Cart.php | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/ViewModel/Recommend/Cart.php b/ViewModel/Recommend/Cart.php index 6a0c8e6fe..da24d7537 100644 --- a/ViewModel/Recommend/Cart.php +++ b/ViewModel/Recommend/Cart.php @@ -3,9 +3,12 @@ namespace Algolia\AlgoliaSearch\ViewModel\Recommend; use Algolia\AlgoliaSearch\Helper\ConfigHelper; +use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper; use Magento\Checkout\Model\Session; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\Block\ArgumentInterface; -use Magento\Framework\View\Element\Template\Context; +use Magento\Store\Model\StoreManagerInterface; class Cart implements ArgumentInterface { @@ -20,34 +23,49 @@ class Cart implements ArgumentInterface protected $configHelper; /** - * @param Context $context + * @var ProductHelper + */ + protected $productHelper; + + /** + * @param StoreManagerInterface $storeManager * @param Session $checkoutSession * @param ConfigHelper $configHelper - * @param array $data + * @param ProductHelper $productHelper */ public function __construct( - Context $context, + StoreManagerInterface $storeManager, Session $checkoutSession, ConfigHelper $configHelper, - array $data = [] + ProductHelper $productHelper ) { + $this->storeManager = $storeManager; $this->checkoutSession = $checkoutSession; $this->configHelper = $configHelper; + $this->productHelper = $productHelper; } /** * @return array - * @throws \Magento\Framework\Exception\LocalizedException - * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws LocalizedException + * @throws NoSuchEntityException */ public function getAllCartItems() { $cartItems = []; + $visibleCartItem = []; $itemCollection = $this->checkoutSession->getQuote()->getAllVisibleItems(); foreach ($itemCollection as $item) { $cartItems[] = $item->getProductId(); } - return array_unique($cartItems); + $storeId = $this->storeManager->getStore()->getId(); + $cartProductCollection = $this->productHelper->getProductCollectionQuery($storeId, array_unique($cartItems)); + if ($cartProductCollection->getSize() > 0 ){ + foreach ($cartProductCollection as $product) { + $visibleCartItem[] = $product->getId(); + } + } + return $visibleCartItem; } /**