Skip to content

Commit 66134b4

Browse files
author
Stanislav Idolov
committed
MAGETWO-58334: [Github] Free shipping is not applied if cart price rule match #6584
1 parent 3524943 commit 66134b4

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\SalesRule\Model\Quote\Address\Total;
8+
9+
class ShippingTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Quote\Api\GuestCartManagementInterface
13+
*/
14+
private $cartManagement;
15+
16+
/**
17+
* @var \Magento\Quote\Api\GuestCartItemRepositoryInterface
18+
*/
19+
private $itemRepository;
20+
21+
/**
22+
* @var \Magento\Framework\ObjectManagerInterface
23+
*/
24+
private $objectManager;
25+
26+
protected function setUp()
27+
{
28+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
29+
$this->cartManagement = $this->objectManager->get(\Magento\Quote\Api\GuestCartManagementInterface::class);
30+
$this->itemRepository = $this->objectManager->get(\Magento\Quote\Api\GuestCartItemRepositoryInterface::class);
31+
}
32+
33+
/**
34+
* @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php
35+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
36+
*/
37+
public function testRuleByProductWeightWithFreeShipping()
38+
{
39+
$cartId = $this->prepareQuote(1);
40+
$methods = $this->estimateShipping($cartId);
41+
42+
$this->assertTrue(count($methods) > 0);
43+
$this->assertEquals('flatrate', $methods[0]->getMethodCode());
44+
$this->assertEquals(0, $methods[0]->getAmount());
45+
46+
}
47+
48+
/**
49+
* @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php
50+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
51+
*/
52+
public function testRuleByProductWeightWithoutFreeShipping()
53+
{
54+
$cartId = $this->prepareQuote(5);
55+
$methods = $this->estimateShipping($cartId);
56+
57+
$this->assertTrue(count($methods) > 0);
58+
$this->assertEquals('flatrate', $methods[0]->getMethodCode());
59+
$this->assertEquals(25, $methods[0]->getAmount());
60+
61+
}
62+
63+
/**
64+
* Estimate shipment for guest cart
65+
*
66+
* @param int $cartId
67+
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
68+
*/
69+
private function estimateShipping($cartId)
70+
{
71+
$addressFactory = $this->objectManager->get(\Magento\Quote\Api\Data\AddressInterfaceFactory::class);
72+
/** @var \Magento\Quote\Api\Data\AddressInterface $address */
73+
$address = $addressFactory->create();
74+
$address->setCountryId('US');
75+
$address->setRegionId(2);
76+
77+
/** @var \Magento\Quote\Api\GuestShipmentEstimationInterface $estimation */
78+
$estimation = $this->objectManager->get(\Magento\Quote\Api\GuestShipmentEstimationInterface::class);
79+
return $estimation->estimateByExtendedAddress($cartId, $address);
80+
}
81+
82+
/**
83+
* Create guest quote with products
84+
*
85+
* @param int $itemQty
86+
* @return int
87+
*/
88+
private function prepareQuote($itemQty)
89+
{
90+
$cartId = $this->cartManagement->createEmptyCart();
91+
92+
/** @var \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory */
93+
$cartItemFactory = $this->objectManager->get(\Magento\Quote\Api\Data\CartItemInterfaceFactory::class);
94+
95+
/** @var \Magento\Quote\Api\Data\CartItemInterface $cartItem */
96+
$cartItem = $cartItemFactory->create();
97+
$cartItem->setQuoteId($cartId);
98+
$cartItem->setQty($itemQty);
99+
$cartItem->setSku('simple');
100+
$cartItem->setProductType(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
101+
102+
$this->itemRepository->save($cartItem);
103+
104+
return $cartId;
105+
}
106+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require 'cart_rule_free_shipping.php';
8+
$row =
9+
[
10+
'name' => 'Free shipping if item weight <= 1',
11+
'conditions' => [
12+
1 =>
13+
[
14+
'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
15+
'attribute' => null,
16+
'operator' => null,
17+
'value' => '1',
18+
'is_value_processed' => null,
19+
'aggregator' => 'all',
20+
'conditions' => [
21+
[
22+
'type' => Magento\SalesRule\Model\Rule\Condition\Address::class,
23+
'attribute' => 'weight',
24+
'operator' => '<=',
25+
'value' => '1',
26+
'is_value_processed' => false,
27+
]
28+
]
29+
]
30+
31+
],
32+
'actions' => [],
33+
];
34+
$salesRule->loadPost($row);
35+
$salesRule->save();

0 commit comments

Comments
 (0)