Skip to content

Commit

Permalink
[GetQuote Loop] #30 - Avoid to use getQuote checkout session method t…
Browse files Browse the repository at this point in the history
…o avoid an infinite loop
  • Loading branch information
PierreLeMaguer authored and maximequeneau committed Oct 5, 2020
1 parent 5f499a1 commit 11022c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Observer/SetNumberOfferedProduct.php
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\CartRepositoryInterface;
use Smile\GiftSalesRule\Helper\GiftRule as GiftRuleHelper;
use Smile\GiftSalesRule\Model\GiftRule;

Expand All @@ -38,18 +39,26 @@ class SetNumberOfferedProduct implements ObserverInterface
*/
protected $giftRuleHelper;

/**
* @var CartRepositoryInterface
*/
protected $quoteRepository;

/**
* SetNumberOfferedProduct constructor.
*
* @param CheckoutSession $checkoutSession Checkout session
* @param GiftRuleHelper $giftRuleHelper Gift rule config helper
* @param CheckoutSession $checkoutSession Checkout session
* @param GiftRuleHelper $giftRuleHelper Gift rule config helper
* @param CartRepositoryInterface $quoteRepository Quote repository
*/
public function __construct(
CheckoutSession $checkoutSession,
GiftRuleHelper $giftRuleHelper
GiftRuleHelper $giftRuleHelper,
CartRepositoryInterface $quoteRepository
) {
$this->checkoutSession = $checkoutSession;
$this->giftRuleHelper = $giftRuleHelper;
$this->quoteRepository = $quoteRepository;
}

/**
Expand All @@ -63,7 +72,7 @@ public function execute(Observer $observer)
$giftRule = $observer->getEvent()->getData('data_object');
$giftRule->setNumberOfferedProduct($giftRule->getMaximumNumberProduct());
try {
$quote = $this->checkoutSession->getQuote();
$quote = $this->quoteRepository->get($this->checkoutSession->getQuoteId());
if (floatval($giftRule->getPriceRange()) > 0) {
$range = $this->giftRuleHelper->getRange($quote, $giftRule);
$giftRule->setNumberOfferedProduct($giftRule->getMaximumNumberProduct() * $range);
Expand Down
5 changes: 5 additions & 0 deletions etc/di.xml
Expand Up @@ -49,6 +49,11 @@
<argument name="checkoutSession" xsi:type="object">Magento\Checkout\Model\Session\Proxy</argument>
</arguments>
</type>
<type name="Smile\GiftSalesRule\Observer\SetNumberOfferedProduct">
<arguments>
<argument name="checkoutSession" xsi:type="object">Magento\Checkout\Model\Session\Proxy</argument>
</arguments>
</type>

<type name="Smile\GiftSalesRule\Helper\GiftRule">
<arguments>
Expand Down

0 comments on commit 11022c2

Please sign in to comment.