Skip to content

Commit

Permalink
Update cart model before collect totals
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolaMalovanets committed Oct 16, 2023
1 parent 5321400 commit ec316fe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Model/Quote/GetQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function getQuote(
try {
$quote = $this->cartRepository->getActive($cartId);
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
$this->shopIdValidator->validate($shopId, $quote->getStoreId());
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
} catch (LocalizedException $e) {
return $this->quoteResultBuilder->createErrorResult($e->getMessage());
}
$quote->collectTotals();
$this->cart->setQuote($quote);
return $this->quoteResultBuilder->createSuccessResult($quote);
}
}
12 changes: 11 additions & 1 deletion Model/Quote/RemoveQuoteCouponCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bold\Checkout\Model\Http\Client\Request\Validator\ShopIdValidator;
use Bold\Checkout\Model\Quote\Result\Builder;
use Exception;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\CouponManagementInterface;
Expand Down Expand Up @@ -48,28 +49,36 @@ class RemoveQuoteCouponCode implements RemoveQuoteCouponCodeInterface
*/
private $checkoutSession;

/**
* @var Cart
*/
private $cart;

/**
* @param ShopIdValidator $shopIdValidator
* @param CouponManagementInterface $couponService
* @param CartRepositoryInterface $cartRepository
* @param Builder $quoteResultBuilder
* @param StoreManagerInterface $storeManager
* @param Session $checkoutSession
* @param Cart $cart
*/
public function __construct(
ShopIdValidator $shopIdValidator,
CouponManagementInterface $couponService,
CartRepositoryInterface $cartRepository,
Builder $quoteResultBuilder,
StoreManagerInterface $storeManager,
Session $checkoutSession
Session $checkoutSession,
Cart $cart
) {
$this->couponService = $couponService;
$this->quoteResultBuilder = $quoteResultBuilder;
$this->cartRepository = $cartRepository;
$this->shopIdValidator = $shopIdValidator;
$this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession;
$this->cart = $cart;
}

/**
Expand All @@ -80,6 +89,7 @@ public function removeCoupon(string $shopId, int $cartId, string $couponCode = n
try {
$quote = $this->cartRepository->getActive($cartId);
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
$this->shopIdValidator->validate($shopId, $quote->getStoreId());
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
Expand Down
2 changes: 1 addition & 1 deletion Model/Quote/SetQuoteAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function setAddresses(
try {
$quote = $this->cartRepository->getActive($cartId);
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
$this->shopIdValidator->validate($shopId, $quote->getStoreId());
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
Expand All @@ -116,7 +117,6 @@ public function setAddresses(
if (!$quote->isVirtual()) {
$this->setShippingAddress($quote, $shippingAddress);
}
$this->cart->setQuote($quote);
$quote->collectTotals();
$this->cartRepository->save($quote);
return $this->quoteResultBuilder->createSuccessResult($quote);
Expand Down
12 changes: 11 additions & 1 deletion Model/Quote/SetQuoteCouponCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Bold\Checkout\Model\Http\Client\Request\Validator\ShopIdValidator;
use Bold\Checkout\Model\Quote\Result\Builder;
use Exception;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\CouponManagementInterface;
Expand Down Expand Up @@ -54,6 +55,11 @@ class SetQuoteCouponCode implements SetQuoteCouponCodeInterface
*/
private $checkoutSession;

/**
* @var Cart
*/
private $cart;

/**
* @param ShopIdValidator $shopIdValidator
* @param CouponManagementInterface $couponService
Expand All @@ -62,6 +68,7 @@ class SetQuoteCouponCode implements SetQuoteCouponCodeInterface
* @param ConfigInterface $config
* @param StoreManagerInterface $storeManager
* @param Session $checkoutSession
* @param Cart $cart used for the backward compatibility with earlier versions of Magento.
*/
public function __construct(
ShopIdValidator $shopIdValidator,
Expand All @@ -70,7 +77,8 @@ public function __construct(
Builder $quoteResultBuilder,
ConfigInterface $config,
StoreManagerInterface $storeManager,
Session $checkoutSession
Session $checkoutSession,
Cart $cart
) {
$this->couponService = $couponService;
$this->quoteResultBuilder = $quoteResultBuilder;
Expand All @@ -79,6 +87,7 @@ public function __construct(
$this->config = $config;
$this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession;
$this->cart = $cart;
}

/**
Expand All @@ -89,6 +98,7 @@ public function setCoupon(string $shopId, int $cartId, string $couponCode): Resu
try {
$quote = $this->cartRepository->getActive($cartId);
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
if ($this->config->isCheckoutTypeSelfHosted((int)$quote->getStore()->getWebsiteId())) {
Expand Down
2 changes: 1 addition & 1 deletion Model/Quote/SetQuoteShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function setShippingMethod(
try {
$quote = $this->cartRepository->getActive($cartId);
$this->checkoutSession->replaceQuote($quote);
$this->cart->setQuote($quote);
$this->shopIdValidator->validate($shopId, $quote->getStoreId());
$this->storeManager->setCurrentStore($quote->getStoreId());
$this->storeManager->getStore()->setCurrentCurrencyCode($quote->getQuoteCurrencyCode());
Expand All @@ -125,7 +126,6 @@ public function setShippingMethod(
if ($this->config->isCheckoutTypeSelfHosted((int)$quote->getStore()->getWebsiteId())) {
$this->quoteResultBuilder->createSuccessResult($quote);
}
$this->cart->setQuote($quote);
$shippingInformation = $this->shippingInformationFactory->create()
->setShippingAddress($quote->getShippingAddress())
->setBillingAddress($quote->getBillingAddress())
Expand Down

0 comments on commit ec316fe

Please sign in to comment.