Skip to content

Commit 6c1aaf0

Browse files
committed
Merge branch 'ACP2E-1290' of https://github.com/magento-l3/magento2ce into PR-01-10-2023
2 parents ff87f8a + 19a35a5 commit 6c1aaf0

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ public function processShippingAmount(Address $address)
372372
$cartRules[$rule->getId()] = $rule->getDiscountAmount();
373373
}
374374
if ($cartRules[$rule->getId()] > 0) {
375-
$shippingQuoteAmount = (float) $address->getShippingAmount()
376-
- (float) $address->getShippingDiscountAmount();
375+
$shippingQuoteAmount = (float) $address->getShippingAmount();
377376
$quoteBaseSubtotal = (float) $quote->getBaseSubtotal();
378377
$isMultiShipping = $this->cartFixedDiscountHelper->checkMultiShippingQuote($quote);
379378
if ($isAppliedToShipping) {

app/code/Magento/SalesRule/Test/Fixture/Rule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ public function apply(array $data = []): ?DataObject
116116

117117
$model->setActionsSerialized($this->serializer->serialize($actions));
118118
$model->setConditionsSerialized($this->serializer->serialize($conditions));
119-
$this->resourceModel->save($model);
119+
//FIXME: plug-ins are configured for \Magento\SalesRule\Model\Rule::save
120+
// instead of \Magento\SalesRule\Model\ResourceModel\Rule::save()
121+
$model->save();
120122

121123
return $model;
122124
}

app/code/Magento/SalesRule/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,4 @@ Apply,Apply
168168
"Trigger recollect totals for quotes by rule ID %1","Trigger recollect totals for quotes by rule ID %1"
169169
"Sorry, something went wrong while triggering recollect totals for affected quotes. Please see log for details.","Sorry, something went wrong while triggering recollect totals for affected quotes. Please see log for details."
170170
"When coupon quantity exceeds %1, the coupon code length must be minimum %2", "When coupon quantity exceeds %1, the coupon code length must be minimum %2"
171+
"Discount amount is distributed among subtotal and shipping amount. Cases when multiple discounts applied to shipping amount are not supported. The option is going to be removed in future releases.ly","Discount amount is distributed among subtotal and shipping amount. Cases when multiple discounts applied to shipping amount are not supported. The option is going to be removed in future releases."

app/code/Magento/SalesRule/view/adminhtml/ui_component/sales_rule_form.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@
472472
<item name="0" xsi:type="string" translate="true">Discount amount is applied to subtotal only</item>
473473
<item name="1" xsi:type="string" translate="true">Discount amount is applied to subtotal and shipping amount separately</item>
474474
</item>
475+
<item name="noticePerSimpleAction" xsi:type="array">
476+
<item name="cart_fixed" xsi:type="string" translate="true">Discount amount is distributed among subtotal and shipping amount. Cases when multiple discounts applied to shipping amount are not supported. The option is going to be removed in future releases.</item>
477+
</item>
475478
</item>
476479
</argument>
477480
<settings>

app/code/Magento/SalesRule/view/adminhtml/web/js/form/element/apply_to_shipping.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ define([
1212
defaults: {
1313
imports: {
1414
toggleDisabled: '${ $.parentName }.simple_action:value'
15-
}
15+
},
16+
noticePerSimpleAction: {},
17+
selectedSimpleAction: ''
1618
},
1719

1820
/**
@@ -29,6 +31,21 @@ define([
2931
if (this.disabled()) {
3032
this.checked(false);
3133
}
34+
this.selectedSimpleAction = action;
35+
this.chooseNotice();
36+
},
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
chooseNotice: function () {
42+
var checkedNoticeNumber = Number(this.checked());
43+
44+
if (checkedNoticeNumber === 1 && this.noticePerSimpleAction.hasOwnProperty(this.selectedSimpleAction)) {
45+
this.notice = this.noticePerSimpleAction[this.selectedSimpleAction];
46+
} else {
47+
this._super();
48+
}
3249
}
3350
});
3451
});

dev/tests/integration/testsuite/Magento/SalesRule/Model/Rule/Action/Discount/CartFixedTest.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1313
use Magento\Catalog\Model\Product\Visibility;
1414
use Magento\Catalog\Model\ProductRepository;
15+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
16+
use Magento\Checkout\Api\Data\TotalsInformationInterface;
1517
use Magento\Checkout\Model\Session as CheckoutSession;
18+
use Magento\Checkout\Model\TotalsInformationManagement;
1619
use Magento\Framework\Api\SearchCriteriaBuilder;
1720
use Magento\Framework\Exception\CouldNotSaveException;
1821
use Magento\Framework\Exception\InputException;
@@ -22,20 +25,28 @@
2225
use Magento\Multishipping\Model\Checkout\Type\Multishipping;
2326
use Magento\Quote\Api\CartRepositoryInterface;
2427
use Magento\Quote\Api\Data\CartItemInterface;
28+
use Magento\Quote\Api\Data\TotalsInterface;
2529
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
2630
use Magento\Quote\Api\GuestCartManagementInterface;
2731
use Magento\Quote\Api\GuestCartTotalRepositoryInterface;
2832
use Magento\Quote\Api\GuestCouponManagementInterface;
2933
use Magento\Quote\Model\Quote;
34+
use Magento\Quote\Model\Quote\Address;
35+
use Magento\Quote\Model\Quote\AddressFactory;
3036
use Magento\Quote\Model\QuoteIdMask;
37+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
38+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
3139
use Magento\Sales\Api\Data\OrderInterface;
3240
use Magento\Sales\Api\OrderRepositoryInterface;
3341
use Magento\Sales\Model\Order;
3442
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
3543
use Magento\SalesRule\Api\RuleRepositoryInterface;
3644
use Magento\SalesRule\Model\Rule;
3745
use Magento\SalesRule\Model\RuleFactory;
46+
use Magento\SalesRule\Test\Fixture\Rule as RuleFixture;
3847
use Magento\Store\Model\StoreManagerInterface;
48+
use Magento\TestFramework\Fixture\DataFixture;
49+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
3950
use Magento\TestFramework\Helper\Bootstrap;
4051
use PHPUnit\Framework\TestCase;
4152

@@ -506,7 +517,7 @@ public function testDiscountsWhenByPercentRuleAppliedFirstAndCartFixedRuleSecond
506517
$expectedDiscounts
507518
): void {
508519
//Update rule discount
509-
/** @var \Magento\SalesRule\Model\Rule $rule */
520+
/** @var Rule $rule */
510521
$rule = $this->getRule('50% off - July 4');
511522
$rule->setDiscountAmount($percentDiscount);
512523
$this->saveRule($rule);
@@ -577,6 +588,41 @@ public function testCartFixedDiscountPriceIncludeTax()
577588
$this->assertEquals(-5, $quote->getShippingAddress()->getDiscountAmount());
578589
}
579590

591+
#[
592+
DataFixture(ProductFixture::class, ['price' => 15], 'p1'),
593+
DataFixture(ProductFixture::class, ['price' => 10], 'p2'),
594+
DataFixture(
595+
RuleFixture::class,
596+
[
597+
'simple_action' => Rule::BY_PERCENT_ACTION,
598+
'discount_amount' => 50,
599+
'apply_to_shipping' => 1,
600+
'stop_rules_processing' => 0,
601+
'sort_order' => 1,
602+
]
603+
),
604+
DataFixture(
605+
RuleFixture::class,
606+
[
607+
'simple_action' => Rule::CART_FIXED_ACTION,
608+
'discount_amount' => 40,
609+
'apply_to_shipping' => 1,
610+
'stop_rules_processing' => 0,
611+
'sort_order' => 2
612+
]
613+
),
614+
DataFixture(GuestCartFixture::class, as: 'cart'),
615+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p1.id$', 'qty' => 2]),
616+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p2.id$', 'qty' => 2])
617+
]
618+
public function testCarFixedDiscountWithApplyToShippingAmountAfterADiscount(): void
619+
{
620+
$cart = DataFixtureStorageManager::getStorage()->get('cart');
621+
$totals = $this->getTotals((int) $cart->getId());
622+
$this->assertEquals(0, $totals->getGrandTotal());
623+
$this->assertEquals(-70, $totals->getDiscountAmount());
624+
}
625+
580626
/**
581627
* Get list of orders by quote id.
582628
*
@@ -632,4 +678,31 @@ private function saveRule(Rule $rule): void
632678
$resourceModel = $this->objectManager->get(\Magento\SalesRule\Model\ResourceModel\Rule::class);
633679
$resourceModel->save($rule);
634680
}
681+
/**
682+
* @param int $cartId
683+
* @return TotalsInterface
684+
*/
685+
private function getTotals(int $cartId): TotalsInterface
686+
{
687+
/** @var Address $address */
688+
$address = $this->objectManager->get(AddressFactory::class)->create();
689+
$totalsManagement = $this->objectManager->get(TotalsInformationManagement::class);
690+
$address->setAddressType(Address::ADDRESS_TYPE_SHIPPING)
691+
->setCountryId('US')
692+
->setRegionId(12)
693+
->setRegion('California')
694+
->setPostcode('90230');
695+
$addressInformation = $this->objectManager->create(
696+
TotalsInformationInterface::class,
697+
[
698+
'data' => [
699+
'address' => $address,
700+
'shipping_method_code' => 'flatrate',
701+
'shipping_carrier_code' => 'flatrate',
702+
],
703+
]
704+
);
705+
706+
return $totalsManagement->calculate($cartId, $addressInformation);
707+
}
635708
}

0 commit comments

Comments
 (0)