Skip to content

Commit b72bd3e

Browse files
committed
Merge branch 'ACP2E-306' of https://github.com/magento-l3/magento2ce into PR-2-2021-25-11
2 parents 20f5116 + 862ab89 commit b72bd3e

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed

app/code/Magento/SalesRule/Model/Rule/Action/Discount/ByFixed.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
class ByFixed extends AbstractDiscount
99
{
1010
/**
11+
* Calculate fixed amount discount
12+
*
1113
* @param \Magento\SalesRule\Model\Rule $rule
1214
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
1315
* @param float $qty
@@ -18,14 +20,23 @@ public function calculate($rule, $item, $qty)
1820
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
1921
$discountData = $this->discountFactory->create();
2022

21-
$quoteAmount = $this->priceCurrency->convert($rule->getDiscountAmount(), $item->getQuote()->getStore());
22-
$discountData->setAmount($qty * $quoteAmount);
23-
$discountData->setBaseAmount($qty * $rule->getDiscountAmount());
23+
$baseDiscountAmount = (float) $rule->getDiscountAmount();
24+
$discountAmount = $this->priceCurrency->convert($baseDiscountAmount, $item->getQuote()->getStore());
25+
$itemPrice = $this->validator->getItemPrice($item);
26+
$baseItemPrice = $this->validator->getItemBasePrice($item);
27+
28+
$discountAmountMin = min($itemPrice * $qty, $discountAmount * $qty);
29+
$baseDiscountAmountMin = min($baseItemPrice * $qty, $baseDiscountAmount * $qty);
30+
31+
$discountData->setAmount($discountAmountMin);
32+
$discountData->setBaseAmount($baseDiscountAmountMin);
2433

2534
return $discountData;
2635
}
2736

2837
/**
38+
* Fix quantity depending on discount step
39+
*
2940
* @param float $qty
3041
* @param \Magento\SalesRule\Model\Rule $rule
3142
* @return float

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CartPromotionsTest.php

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\TestFramework\TestCase\GraphQlAbstract;
1717
use Magento\Tax\Model\ClassModel as TaxClassModel;
1818
use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
19+
use Magento\SalesRule\Api\RuleRepositoryInterface;
1920

2021
/**
2122
* Test cases for applying cart promotions to items in cart
@@ -372,6 +373,73 @@ public function testCartPromotionsWithNoRuleLabels()
372373
}
373374
}
374375

376+
/**
377+
* Test fixed discount cannot be higher than products price
378+
*
379+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
380+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
381+
*/
382+
public function testCartPromotionsFixedDiscountNotHigherThanProductsPrice()
383+
{
384+
/** @var ProductRepositoryInterface $productRepository */
385+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
386+
/** @var Product $prod2 */
387+
$prod1 = $productRepository->get('simple1');
388+
$prod2 = $productRepository->get('simple2');
389+
$productsInCart = [$prod1, $prod2];
390+
$skus =['simple1', 'simple2'];
391+
$qty = 2;
392+
$sumOfPricesForBothProducts = 43.96;
393+
$couponCode = '2?ds5!2d';
394+
/** @var RuleRepositoryInterface $ruleRepository */
395+
$ruleRepository = Bootstrap::getObjectManager()->get(RuleRepositoryInterface::class);
396+
/** @var Collection $ruleCollection */
397+
$ruleCollection = Bootstrap::getObjectManager()->get(Collection::class);
398+
$ruleLabels = [];
399+
/** @var Rule $rule */
400+
foreach ($ruleCollection as $rule) {
401+
$ruleLabels = $rule->getStoreLabels();
402+
$salesRule = $ruleRepository->getById($rule->getRuleId());
403+
$salesRule->setDiscountAmount(50);
404+
$ruleRepository->save($salesRule);
405+
}
406+
$cartId = $this->createEmptyCart();
407+
$this->addMultipleSimpleProductsToCart($cartId, $qty, $skus[0], $skus[1]);
408+
$this->applyCouponsToCart($cartId, $couponCode);
409+
$query = $this->getCartItemPricesQuery($cartId);
410+
$response = $this->graphQlMutation($query);
411+
412+
$this->assertCount(2, $response['cart']['items']);
413+
$productsInResponse = array_map(null, $response['cart']['items'], $productsInCart);
414+
$count = count($productsInCart);
415+
for ($itemIndex = 0; $itemIndex < $count; $itemIndex++) {
416+
$this->assertNotEmpty($productsInResponse[$itemIndex]);
417+
$rowTotal = ($productsInCart[$itemIndex]->getSpecialPrice()*$qty);
418+
$this->assertResponseFields(
419+
$productsInResponse[$itemIndex][0],
420+
[
421+
'quantity' => $qty,
422+
'prices' => [
423+
'row_total' => ['value' => $rowTotal],
424+
'row_total_including_tax' => ['value' => $rowTotal],
425+
'total_item_discount' => ['value' => $rowTotal],
426+
'discounts' => [
427+
0 =>[
428+
'amount' =>
429+
['value' => $rowTotal],
430+
'label' => $ruleLabels[0]
431+
]
432+
]
433+
],
434+
]
435+
);
436+
}
437+
$this->assertEquals(
438+
$response['cart']['prices']['discounts'][0]['amount']['value'],
439+
$sumOfPricesForBothProducts
440+
);
441+
}
442+
375443
/**
376444
* Apply coupon to the cart
377445
*
@@ -426,7 +494,6 @@ private function getCartItemPricesQuery(string $cartId): string
426494
discounts{
427495
amount{value}
428496
}
429-
430497
}
431498
}
432499
}
@@ -460,27 +527,27 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
460527
$query = <<<QUERY
461528
mutation {
462529
addSimpleProductsToCart(input: {
463-
cart_id: "{$cartId}",
530+
cart_id: "{$cartId}",
464531
cart_items: [
465532
{
466533
data: {
467534
quantity: $qty
468535
sku: "$sku1"
469536
}
470-
}
537+
}
471538
{
472539
data: {
473540
quantity: $qty
474541
sku: "$sku2"
475542
}
476-
}
543+
}
477544
]
478545
}
479546
) {
480547
cart {
481548
items {
482549
product{sku}
483-
quantity
550+
quantity
484551
}
485552
}
486553
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Magento\Framework\Registry;
10+
use Magento\SalesRule\Api\RuleRepositoryInterface;
11+
use Magento\SalesRule\Model\Rule;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
16+
/** @var Registry $registry */
17+
$registry = $objectManager->get(Registry::class);
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', true);
20+
21+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
22+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
23+
$searchCriteria = $searchCriteriaBuilder->addFilter('name', '10% Off on orders with two items')
24+
->create();
25+
/** @var RuleRepositoryInterface $ruleRepository */
26+
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
27+
$items = $ruleRepository->getList($searchCriteria)
28+
->getItems();
29+
/** @var Rule $salesRule */
30+
$salesRule = array_pop($items);
31+
if ($salesRule !== null) {
32+
$ruleRepository->deleteById($salesRule->getRuleId());
33+
}
34+
35+
$registry->unregister('isSecureArea');
36+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)