|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Checkout\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Framework\Stdlib\DateTime\DateTime; |
| 12 | +use Magento\Quote\Api\CartRepositoryInterfaceFactory; |
| 13 | +use Magento\Quote\Api\Data\CartInterface; |
| 14 | +use Magento\Quote\Model\QuoteMutexInterface; |
| 15 | + |
| 16 | +class AddProductToCart |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param QuoteMutexInterface $quoteMutex |
| 20 | + * @param CartRepositoryInterfaceFactory $quoteRepositoryFactory |
| 21 | + * @param DateTime $dateTime |
| 22 | + */ |
| 23 | + public function __construct( |
| 24 | + private readonly QuoteMutexInterface $quoteMutex, |
| 25 | + private readonly CartRepositoryInterfaceFactory $quoteRepositoryFactory, |
| 26 | + private readonly DateTime $dateTime |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Add product to cart |
| 32 | + * |
| 33 | + * @param Cart $cart |
| 34 | + * @param Product $product |
| 35 | + * @param array $buyRequest Buy request info |
| 36 | + * @param array $related Product IDs to add to the cart along with the main product |
| 37 | + * @return bool |
| 38 | + * @throws \Throwable |
| 39 | + */ |
| 40 | + public function execute(Cart $cart, Product $product, array $buyRequest = [], array $related = []): bool |
| 41 | + { |
| 42 | + if (!$cart->getQuote()->getId()) { |
| 43 | + return $this->add($cart, $product, $buyRequest, $related); |
| 44 | + } |
| 45 | + |
| 46 | + return $this->quoteMutex->execute( |
| 47 | + [(int) $cart->getQuote()->getId()], |
| 48 | + function (array $quotes = []) use ($cart, $product, $buyRequest, $related) { |
| 49 | + $reload = true; |
| 50 | + // check if the mutex provided the quote |
| 51 | + if (!empty($quotes)) { |
| 52 | + // check if the quote was updated since the last load |
| 53 | + // if not, we can use the quote in memory to avoid full reload which is expensive and unnecessary |
| 54 | + $lastUpdatedAt = $cart->getQuote()->getUpdatedAt() |
| 55 | + ?: $cart->getQuote()->getOrigData(CartInterface::KEY_UPDATED_AT); |
| 56 | + $quote = current($quotes); |
| 57 | + $updatedAt = $quote->getUpdatedAt(); |
| 58 | + $reload = $updatedAt |
| 59 | + && $lastUpdatedAt |
| 60 | + && $this->dateTime->timestamp($updatedAt) > $this->dateTime->timestamp($lastUpdatedAt); |
| 61 | + } |
| 62 | + if ($reload) { |
| 63 | + // bypass repository cache by creating a new repository instead of using the shared repository |
| 64 | + $quote = $this->quoteRepositoryFactory->create()->getActive($cart->getQuote()->getId()); |
| 65 | + $cart->setQuote($quote); |
| 66 | + $cart->getCheckoutSession()->replaceQuote($quote); |
| 67 | + } |
| 68 | + return $this->add($cart, $product, $buyRequest, $related); |
| 69 | + }, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add product to cart |
| 75 | + * |
| 76 | + * @param Cart $cart |
| 77 | + * @param Product $product |
| 78 | + * @param array $buyRequest |
| 79 | + * @param array $related |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + private function add(Cart $cart, Product $product, array $buyRequest, array $related = []): bool |
| 83 | + { |
| 84 | + $cart->addProduct($product, $buyRequest); |
| 85 | + if (!empty($related)) { |
| 86 | + $cart->addProductsByIds($related); |
| 87 | + } |
| 88 | + $cart->save(); |
| 89 | + return true; |
| 90 | + } |
| 91 | +} |
0 commit comments