Skip to content

Commit e0e4733

Browse files
authored
BXGY detection (#717)
* BXGY detection * Lint fixes
1 parent 750ec9d commit e0e4733

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

app/code/Meta/Sales/Model/Api/CartTotalsApi.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
use Magento\Framework\Exception\NoSuchEntityException;
2525
use Magento\Quote\Api\Data\TotalsInterface;
2626
use Magento\Quote\Model\GuestCart\GuestCartTotalRepository;
27+
use Magento\Quote\Model\Quote;
28+
use Magento\SalesRule\Model\Rule;
29+
use Magento\SalesRule\Api\RuleRepositoryInterface;
2730
use Meta\BusinessExtension\Helper\FBEHelper;
2831
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
2932
use Meta\Sales\Api\CartTotalsApiInterface;
3033
use Meta\Sales\Helper\OrderHelper;
34+
use Magento\Quote\Model\QuoteRepository;
35+
use Magento\Quote\Model\QuoteIdMaskFactory;
3136

3237
class CartTotalsApi implements CartTotalsApiInterface
3338
{
@@ -51,22 +56,46 @@ class CartTotalsApi implements CartTotalsApiInterface
5156
*/
5257
private FBEHelper $fbeHelper;
5358

59+
/**
60+
* @var QuoteRepository
61+
*/
62+
private QuoteRepository $quoteRepository;
63+
64+
/**
65+
* @var RuleRepositoryInterface
66+
*/
67+
private RuleRepositoryInterface $ruleRepository;
68+
69+
/**
70+
* @var QuoteIdMaskFactory
71+
*/
72+
private QuoteIdMaskFactory $quoteIdMaskFactory;
73+
5474
/**
5575
* @param Authenticator $authenticator
5676
* @param OrderHelper $orderHelper
5777
* @param GuestCartTotalRepository $guestCartTotalRepository
5878
* @param FBEHelper $fbeHelper
79+
* @param QuoteRepository $quoteRepository
80+
* @param RuleRepositoryInterface $ruleRepository
81+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
5982
*/
6083
public function __construct(
6184
Authenticator $authenticator,
6285
OrderHelper $orderHelper,
6386
GuestCartTotalRepository $guestCartTotalRepository,
64-
FBEHelper $fbeHelper
87+
FBEHelper $fbeHelper,
88+
QuoteRepository $quoteRepository,
89+
RuleRepositoryInterface $ruleRepository,
90+
QuoteIdMaskFactory $quoteIdMaskFactory
6591
) {
6692
$this->authenticator = $authenticator;
6793
$this->orderHelper = $orderHelper;
6894
$this->guestCartTotalRepository = $guestCartTotalRepository;
6995
$this->fbeHelper = $fbeHelper;
96+
$this->quoteRepository = $quoteRepository;
97+
$this->ruleRepository = $ruleRepository;
98+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
7099
}
71100

72101
/**
@@ -83,7 +112,25 @@ public function getCartTotals(string $externalBusinessId, string $cartId): Total
83112
$this->authenticator->authenticateRequest();
84113
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
85114
try {
86-
return $this->guestCartTotalRepository->get($cartId);
115+
$quoteId = (int)$this->quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getQuoteId();
116+
/**
117+
* @var Quote $quote
118+
*/
119+
$quote = $this->quoteRepository->get($quoteId);
120+
$totals = $this->guestCartTotalRepository->get($cartId);
121+
$rules = explode(',', $quote->getAppliedRuleIds() ?? "");
122+
foreach ($rules as $ruleId) {
123+
if ($ruleId) {
124+
$rule = $this->ruleRepository->getById($ruleId);
125+
if ($rule->getSimpleAction() === Rule::BUY_X_GET_Y_ACTION) {
126+
$attrs = $totals->getExtensionAttributes();
127+
$attrs->setBxgyDiscountApplied(true);
128+
$totals->setExtensionAttributes($attrs);
129+
break 1; // Exit
130+
}
131+
}
132+
}
133+
return $totals;
87134
} catch (NoSuchEntityException $e) {
88135
$le = new LocalizedException(
89136
__(

app/code/Meta/Sales/etc/extension_attributes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
<attribute code="email_remarketing_option" type="string"/>
88
<attribute code="synced_shipments" type="mixed[]"/>
99
</extension_attributes>
10+
<extension_attributes for="Magento\Quote\Api\Data\TotalsInterface">
11+
<attribute code="bxgy_discount_applied" type="boolean" />
12+
</extension_attributes>
1013
</config>

0 commit comments

Comments
 (0)