From 140b2ce4b8e415e937dbb77b3838bfd439dcce9d Mon Sep 17 00:00:00 2001 From: Damien Couchez Date: Mon, 6 Jan 2025 15:47:34 +0100 Subject: [PATCH] MAGE-1097: add a plugin to forward insights information --- Plugin/AddToCartRedirectForInsights.php | 194 ++++++++++++++++++++++ etc/frontend/di.xml | 4 + view/frontend/templates/instant/hit.phtml | 2 + 3 files changed, 200 insertions(+) create mode 100644 Plugin/AddToCartRedirectForInsights.php diff --git a/Plugin/AddToCartRedirectForInsights.php b/Plugin/AddToCartRedirectForInsights.php new file mode 100644 index 000000000..0da3ddaed --- /dev/null +++ b/Plugin/AddToCartRedirectForInsights.php @@ -0,0 +1,194 @@ +getProduct($productInfo); + $productId = $product->getId(); + + if ($productId) { + $request = $this->getQtyRequest($product, $requestInfo); + + try { + $result = $product->getTypeInstance()->prepareForCartAdvanced($request, $product); + } catch (LocalizedException $e) { + $this->checkoutSession->setUseNotice(false); + $result = $e->getMessage(); + } + + // if the result is a string, this mean that the product can't be added to the cart + // see Magento\Quote\Model\Quote::addProduct() + // Here we need to add the insights information to the redirect + if (is_string($result)) { + $redirectUrl = $product->getUrlModel()->getUrl( + $product, + [ + '_query' => [ + 'objectID' => $product->getId(), + 'queryID' => $requestInfo['queryID'], + 'indexName' => $requestInfo['indexName'] + ] + ] + ); + + $this->checkoutSession->setRedirectUrl($redirectUrl); + if ($this->checkoutSession->getUseNotice() === null) { + $this->checkoutSession->setUseNotice(true); + } + throw new LocalizedException(__($result)); + } + } + } + + /** + * @param $productInfo + * + * @return Product + * + * @throws NoSuchEntityException + * @throws LocalizedException + */ + protected function getProduct($productInfo): Product + { + $product = null; + if ($productInfo instanceof Product) { + $product = $productInfo; + if (!$product->getId()) { + throw new LocalizedException( + __("The product wasn't found. Verify the product and try again.") + ); + } + } elseif (is_int($productInfo) || is_string($productInfo)) { + $storeId = $this->storeManager->getStore()->getId(); + try { + $product = $this->productRepository->getById($productInfo, false, $storeId); + } catch (NoSuchEntityException $e) { + throw new LocalizedException( + __("The product wasn't found. Verify the product and try again."), + $e + ); + } + } else { + throw new LocalizedException( + __("The product wasn't found. Verify the product and try again.") + ); + } + $currentWebsiteId = $this->storeManager->getStore()->getWebsiteId(); + if (!is_array($product->getWebsiteIds()) || !in_array($currentWebsiteId, $product->getWebsiteIds())) { + throw new LocalizedException( + __("The product wasn't found. Verify the product and try again.") + ); + } + return $product; + } + + /** + * Get request quantity + * + * @param Product $product + * @param DataObject|int|array $request + * @return int|DataObject + */ + protected function getQtyRequest($product, $request = 0) + { + $request = $this->getProductRequest($request); + $stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId()); + $minimumQty = $stockItem->getMinSaleQty(); + //If product quantity is not specified in request and there is set minimal qty for it + if ($minimumQty + && $minimumQty > 0 + && !$request->getQty() + ) { + $request->setQty($minimumQty); + } + + return $request; + } + + /** + * Get request for product add to cart procedure + * + * @param DataObject|int|array $requestInfo + * @return DataObject + * @throws LocalizedException + */ + protected function getProductRequest($requestInfo) + { + if ($requestInfo instanceof DataObject) { + $request = $requestInfo; + } elseif (is_numeric($requestInfo)) { + $request = new DataObject(['qty' => $requestInfo]); + } elseif (is_array($requestInfo)) { + $request = new DataObject($requestInfo); + } else { + throw new LocalizedException( + __('We found an invalid request for adding product to quote.') + ); + } + $this->getRequestInfoFilter()->filter($request); + + return $request; + } + + /** + * Getter for RequestInfoFilter + * + * @return RequestInfoFilterInterface + */ + protected function getRequestInfoFilter() + { + if ($this->requestInfoFilter === null) { + $this->requestInfoFilter = \Magento\Framework\App\ObjectManager::getInstance() + ->get(RequestInfoFilterInterface::class); + } + return $this->requestInfoFilter; + } +} diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index d14ad0439..35f4fd0f5 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -20,4 +20,8 @@ + + + + diff --git a/view/frontend/templates/instant/hit.phtml b/view/frontend/templates/instant/hit.phtml index fb65c7943..ecd3f3e0c 100644 --- a/view/frontend/templates/instant/hit.phtml +++ b/view/frontend/templates/instant/hit.phtml @@ -85,6 +85,8 @@ $origFormatedVar = $block->escapeHtml('price' . $priceKey . '_original_formated'
+ + {{#_highlightResult.default_bundle_options}}{{/_highlightResult.default_bundle_options}}