Skip to content

Commit d699cca

Browse files
committed
Merge branch 'ACP2E-2044' of https://github.com/magento-l3/magento2ce into PR-07032023
2 parents 52ece9e + 997944e commit d699cca

File tree

8 files changed

+255
-33
lines changed

8 files changed

+255
-33
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class Discounts implements ResolverInterface
2020
{
21+
public const TYPE_SHIPPING = "SHIPPING";
22+
public const TYPE_ITEM = "ITEM";
2123
/**
2224
* @inheritdoc
2325
*/
@@ -41,21 +43,22 @@ private function getDiscountValues(Quote $quote)
4143
{
4244
$discountValues=[];
4345
$address = $quote->getShippingAddress();
44-
$totals = $address->getTotals();
45-
if ($totals && is_array($totals)) {
46-
foreach ($totals as $total) {
47-
if (stripos($total->getCode(), 'total') === false && $total->getValue() < 0.00) {
48-
$discount = [];
49-
$amount = [];
50-
$discount['label'] = $total->getTitle() ?: __('Discount');
51-
$amount['value'] = $total->getValue() * -1;
52-
$amount['currency'] = $quote->getQuoteCurrencyCode();
53-
$discount['amount'] = $amount;
54-
$discountValues[] = $discount;
55-
}
46+
$totalDiscounts = $address->getExtensionAttributes()->getDiscounts();
47+
48+
if ($totalDiscounts && is_array($totalDiscounts)) {
49+
foreach ($totalDiscounts as $value) {
50+
$discount = [];
51+
$amount = [];
52+
$discount['label'] = $value->getRuleLabel() ?: __('Discount');
53+
/* @var \Magento\SalesRule\Api\Data\DiscountDataInterface $discountData */
54+
$discountData = $value->getDiscountData();
55+
$discount['applied_to'] = $discountData->getAppliedTo();
56+
$amount['value'] = $discountData->getAmount();
57+
$amount['currency'] = $quote->getQuoteCurrencyCode();
58+
$discount['amount'] = $amount;
59+
$discountValues[] = $discount;
5660
}
57-
return $discountValues;
5861
}
59-
return null;
62+
return $discountValues ?: null;
6063
}
6164
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,17 @@ enum CartItemErrorType {
359359
ITEM_INCREMENTS
360360
}
361361

362-
type Discount @doc(description:"Defines an individual discount. A discount can be applied to the cart as a whole or to an item.") {
362+
type Discount @doc(description:"Defines an individual discount. A discount can be applied to the cart as a whole or to an item, shipping.") {
363363
amount: Money! @doc(description:"The amount of the discount.")
364+
applied_to: CartDiscountType! @doc(description:"The type of the entity the discount is applied to.")
364365
label: String! @doc(description:"A description of the discount.")
365366
}
366367

368+
enum CartDiscountType {
369+
ITEM
370+
SHIPPING
371+
}
372+
367373
type CartItemPrices @doc(description: "Contains details about the price of the item, including taxes and discounts.") {
368374
price: Money! @doc(description: "The price of the item before any discounts were applied. The price that might include tax, depending on the configured display settings for cart.")
369375
price_including_tax: Money! @doc(description: "The price of the item before any discounts were applied. The price that might include tax, depending on the configured display settings for cart.")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Api\Data;
9+
10+
interface DiscountAppliedToInterface
11+
{
12+
public const APPLIED_TO_ITEM = 'ITEM';
13+
public const APPLIED_TO_SHIPPING = 'SHIPPING';
14+
public const APPLIED_TO = 'applied_to';
15+
/**
16+
* Get entity type the diescount is applied to
17+
*
18+
* @return string
19+
*/
20+
public function getAppliedTo();
21+
}

app/code/Magento/SalesRule/Model/Data/DiscountData.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
namespace Magento\SalesRule\Model\Data;
99

1010
use Magento\SalesRule\Api\Data\DiscountDataInterface;
11+
use Magento\SalesRule\Api\Data\DiscountAppliedToInterface;
1112
use Magento\Framework\Api\ExtensionAttributesInterface;
1213

1314
/**
1415
* Discount Data Model
1516
*/
16-
class DiscountData extends \Magento\Framework\Api\AbstractExtensibleObject implements DiscountDataInterface
17+
class DiscountData extends \Magento\Framework\Api\AbstractExtensibleObject implements
18+
DiscountDataInterface,
19+
DiscountAppliedToInterface
1720
{
1821

19-
const AMOUNT = 'amount';
20-
const BASE_AMOUNT = 'base_amount';
21-
const ORIGINAL_AMOUNT = 'original_amount';
22-
const BASE_ORIGINAL_AMOUNT = 'base_original_amount';
22+
public const AMOUNT = 'amount';
23+
public const BASE_AMOUNT = 'base_amount';
24+
public const ORIGINAL_AMOUNT = 'original_amount';
25+
public const BASE_ORIGINAL_AMOUNT = 'base_original_amount';
2326

2427
/**
2528
* Get Amount
@@ -126,4 +129,14 @@ public function setExtensionAttributes(
126129
) {
127130
return $this->_setExtensionAttributes($extensionAttributes);
128131
}
132+
133+
/**
134+
* Get entity type the discount is applied to
135+
*
136+
* @return string
137+
*/
138+
public function getAppliedTo()
139+
{
140+
return $this->_get(DiscountAppliedToInterface::APPLIED_TO) ?: DiscountAppliedToInterface::APPLIED_TO_ITEM;
141+
}
129142
}

app/code/Magento/SalesRule/Model/RulesApplier.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
1616
use Magento\SalesRule\Api\Data\RuleDiscountInterfaceFactory;
1717
use Magento\SalesRule\Api\Data\DiscountDataInterfaceFactory;
18+
use Magento\SalesRule\Api\Data\DiscountAppliedToInterface as DiscountAppliedTo;
1819

1920
/**
2021
* Rule applier model
@@ -150,6 +151,26 @@ public function applyRules($item, $rules, $skipValidation, $couponCode)
150151
public function addDiscountDescription($address, $rule)
151152
{
152153
$description = $address->getDiscountDescriptionArray();
154+
$label = $this->getRuleLabel($address, $rule);
155+
156+
if (strlen($label)) {
157+
$description[$rule->getId()] = $label;
158+
}
159+
160+
$address->setDiscountDescriptionArray($description);
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* Retrieve rule label
167+
*
168+
* @param Address $address
169+
* @param Rule $rule
170+
* @return string
171+
*/
172+
private function getRuleLabel(Address $address, Rule $rule): string
173+
{
153174
$ruleLabel = $rule->getStoreLabel($address->getQuote()->getStore());
154175
$label = '';
155176
if ($ruleLabel) {
@@ -163,14 +184,30 @@ public function addDiscountDescription($address, $rule)
163184
}
164185
}
165186
}
187+
return $label;
188+
}
166189

167-
if (strlen($label)) {
168-
$description[$rule->getId()] = $label;
169-
}
170-
171-
$address->setDiscountDescriptionArray($description);
172-
173-
return $this;
190+
/**
191+
* Add rule shipping discount description label to address object
192+
*
193+
* @param Address $address
194+
* @param Rule $rule
195+
* @param array $discount
196+
* @return void
197+
*/
198+
public function addShippingDiscountDescription(Address $address, Rule $rule, array $discount): void
199+
{
200+
$addressDiscounts = $address->getExtensionAttributes()->getDiscounts();
201+
$ruleLabel = $this->getRuleLabel($address, $rule);
202+
$discount[DiscountAppliedTo::APPLIED_TO] = DiscountAppliedTo::APPLIED_TO_SHIPPING;
203+
$discountData = $this->discountDataInterfaceFactory->create(['data' => $discount]);
204+
$data = [
205+
'discount' => $discountData,
206+
'rule' => $ruleLabel,
207+
'rule_id' => $rule->getRuleId(),
208+
];
209+
$addressDiscounts[] = $this->discountInterfaceFactory->create(['data' => $data]);
210+
$address->getExtensionAttributes()->setDiscounts($addressDiscounts);
174211
}
175212

176213
/**

app/code/Magento/SalesRule/Model/Validator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,13 @@ public function processShippingAmount(Address $address)
421421
$baseDiscountAmount = $quoteAmount;
422422
break;
423423
}
424-
424+
if ($address->getShippingDiscountAmount() + $discountAmount <= $shippingAmount) {
425+
$data = [
426+
'amount' => $discountAmount,
427+
'base_amount' => $baseDiscountAmount
428+
];
429+
$this->rulesApplier->addShippingDiscountDescription($address, $rule, $data);
430+
}
425431
$discountAmount = min($address->getShippingDiscountAmount() + $discountAmount, $shippingAmount);
426432
$baseDiscountAmount = min(
427433
$address->getBaseShippingDiscountAmount() + $baseDiscountAmount,
@@ -440,7 +446,6 @@ public function processShippingAmount(Address $address)
440446

441447
$address->setAppliedRuleIds($this->validatorUtility->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
442448
$quote->setAppliedRuleIds($this->validatorUtility->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
443-
444449
return $this;
445450
}
446451

app/code/Magento/SalesRule/Test/Unit/Model/ValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function setUp(): void
110110
$this->helper = new ObjectManager($this);
111111
$this->rulesApplier = $this->createPartialMock(
112112
RulesApplier::class,
113-
['setAppliedRuleIds', 'applyRules', 'addDiscountDescription']
113+
['setAppliedRuleIds', 'applyRules', 'addDiscountDescription', 'addShippingDiscountDescription']
114114
);
115115

116116
$this->addressMock = $this->getMockBuilder(Address::class)

0 commit comments

Comments
 (0)