Skip to content

Commit

Permalink
Merge pull request #5 from hyva-themes/4/customer-masked-id-fix
Browse files Browse the repository at this point in the history
#4 Create a quote maskId is none exists
  • Loading branch information
wigman committed Feb 3, 2021
2 parents ecaec74 + 732bd07 commit 9fd3771
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/CustomerData/CartPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;

class CartPlugin
Expand Down Expand Up @@ -46,19 +48,25 @@ class CartPlugin
* @var CurrentCustomer
*/
private $currentCustomer;
/**
* @var quoteIdMaskFactory
*/
private $quoteIdMaskFactory;

public function __construct(
Session $checkoutSession,
CurrentCustomer $currentCustomer,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Checkout\Model\Cart $checkoutCart,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
quoteIdMaskFactory $quoteIdMaskFactory
) {
$this->checkoutSession = $checkoutSession;
$this->checkoutCart = $checkoutCart;
$this->storeManager = $storeManager;
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
$this->currentCustomer = $currentCustomer;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}

/**
Expand All @@ -72,12 +80,21 @@ public function afterGetSectionData(Cart $subject, array $result)
{
$storeViewCode = (string) $this->storeManager->getStore()->getCode();
$cartId = (int) $this->getQuote()->getId();
$maskedCartId = '';

if ($cartId) {
$cartId = $this->getQuoteMaskId($cartId) ?: $cartId;
$maskedCartId = $this->getQuoteMaskId($cartId);

if (!$maskedCartId) {
/** @var QuoteIdMask $quoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($cartId)
->save();
$maskedCartId = $quoteIdMask->getMaskedId() ?: '';
}
}

$result['cartId'] = $cartId;
$result['cartId'] = $maskedCartId;
$result['storeViewCode'] = $storeViewCode;

return $result;
Expand Down

0 comments on commit 9fd3771

Please sign in to comment.