|
12 | 12 | use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
13 | 13 | use Magento\Catalog\Model\Product\Visibility;
|
14 | 14 | use Magento\Catalog\Model\ProductRepository;
|
| 15 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 16 | +use Magento\Checkout\Api\Data\TotalsInformationInterface; |
15 | 17 | use Magento\Checkout\Model\Session as CheckoutSession;
|
| 18 | +use Magento\Checkout\Model\TotalsInformationManagement; |
16 | 19 | use Magento\Framework\Api\SearchCriteriaBuilder;
|
17 | 20 | use Magento\Framework\Exception\CouldNotSaveException;
|
18 | 21 | use Magento\Framework\Exception\InputException;
|
|
22 | 25 | use Magento\Multishipping\Model\Checkout\Type\Multishipping;
|
23 | 26 | use Magento\Quote\Api\CartRepositoryInterface;
|
24 | 27 | use Magento\Quote\Api\Data\CartItemInterface;
|
| 28 | +use Magento\Quote\Api\Data\TotalsInterface; |
25 | 29 | use Magento\Quote\Api\GuestCartItemRepositoryInterface;
|
26 | 30 | use Magento\Quote\Api\GuestCartManagementInterface;
|
27 | 31 | use Magento\Quote\Api\GuestCartTotalRepositoryInterface;
|
28 | 32 | use Magento\Quote\Api\GuestCouponManagementInterface;
|
29 | 33 | use Magento\Quote\Model\Quote;
|
| 34 | +use Magento\Quote\Model\Quote\Address; |
| 35 | +use Magento\Quote\Model\Quote\AddressFactory; |
30 | 36 | use Magento\Quote\Model\QuoteIdMask;
|
| 37 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 38 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
31 | 39 | use Magento\Sales\Api\Data\OrderInterface;
|
32 | 40 | use Magento\Sales\Api\OrderRepositoryInterface;
|
33 | 41 | use Magento\Sales\Model\Order;
|
34 | 42 | use Magento\Sales\Model\Order\Email\Sender\OrderSender;
|
35 | 43 | use Magento\SalesRule\Api\RuleRepositoryInterface;
|
36 | 44 | use Magento\SalesRule\Model\Rule;
|
37 | 45 | use Magento\SalesRule\Model\RuleFactory;
|
| 46 | +use Magento\SalesRule\Test\Fixture\Rule as RuleFixture; |
38 | 47 | use Magento\Store\Model\StoreManagerInterface;
|
| 48 | +use Magento\TestFramework\Fixture\DataFixture; |
| 49 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
39 | 50 | use Magento\TestFramework\Helper\Bootstrap;
|
40 | 51 | use PHPUnit\Framework\TestCase;
|
41 | 52 |
|
@@ -506,7 +517,7 @@ public function testDiscountsWhenByPercentRuleAppliedFirstAndCartFixedRuleSecond
|
506 | 517 | $expectedDiscounts
|
507 | 518 | ): void {
|
508 | 519 | //Update rule discount
|
509 |
| - /** @var \Magento\SalesRule\Model\Rule $rule */ |
| 520 | + /** @var Rule $rule */ |
510 | 521 | $rule = $this->getRule('50% off - July 4');
|
511 | 522 | $rule->setDiscountAmount($percentDiscount);
|
512 | 523 | $this->saveRule($rule);
|
@@ -577,6 +588,41 @@ public function testCartFixedDiscountPriceIncludeTax()
|
577 | 588 | $this->assertEquals(-5, $quote->getShippingAddress()->getDiscountAmount());
|
578 | 589 | }
|
579 | 590 |
|
| 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 | + |
580 | 626 | /**
|
581 | 627 | * Get list of orders by quote id.
|
582 | 628 | *
|
@@ -632,4 +678,31 @@ private function saveRule(Rule $rule): void
|
632 | 678 | $resourceModel = $this->objectManager->get(\Magento\SalesRule\Model\ResourceModel\Rule::class);
|
633 | 679 | $resourceModel->save($rule);
|
634 | 680 | }
|
| 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 | + } |
635 | 708 | }
|
0 commit comments