Skip to content

Commit 4dcc18a

Browse files
committed
MAGETWO-98680: Cart Price Rule code is absent on "view Order/Invoice" Admin page if had been used for Free Shipping
1 parent 19e9ada commit 4dcc18a

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

app/code/Magento/Sales/Block/Adminhtml/Totals.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml;
77

8+
use Magento\Sales\Model\Order;
9+
810
class Totals extends \Magento\Sales\Block\Order\Totals
911
{
1012
/**
@@ -67,12 +69,16 @@ protected function _initTotals()
6769
if (!$this->getSource()->getIsVirtual() && ((double)$this->getSource()->getShippingAmount() ||
6870
$this->getSource()->getShippingDescription())
6971
) {
72+
$shippingLabel = __('Shipping & Handling');
73+
if ($this->isHasFreeShipping($this->getSource()) && $this->getSource()->getDiscountDescription()) {
74+
$shippingLabel .= sprintf(' (%s)', $this->getSource()->getDiscountDescription());
75+
}
7076
$this->_totals['shipping'] = new \Magento\Framework\DataObject(
7177
[
7278
'code' => 'shipping',
7379
'value' => $this->getSource()->getShippingAmount(),
7480
'base_value' => $this->getSource()->getBaseShippingAmount(),
75-
'label' => __('Shipping & Handling'),
81+
'label' => $shippingLabel,
7682
]
7783
);
7884
}
@@ -109,4 +115,23 @@ protected function _initTotals()
109115

110116
return $this;
111117
}
118+
119+
/**
120+
* Availability of free shipping in at least one order item
121+
*
122+
* @param Order $order
123+
* @return bool
124+
*/
125+
private function isHasFreeShipping(Order $order): bool
126+
{
127+
$isActive = false;
128+
foreach ($order->getItems() as $orderItem) {
129+
if ($orderItem->getFreeShipping() === '1') {
130+
$isActive = true;
131+
break;
132+
}
133+
}
134+
135+
return $isActive;
136+
}
112137
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Block\Adminhtml;
7+
8+
use Magento\Framework\View\LayoutInterface;
9+
use Magento\Sales\Model\Order;
10+
use Magento\Sales\Model\OrderFactory;
11+
12+
/**
13+
* Test class for \Magento\Sales\Block\Adminhtml\Totals
14+
*/
15+
class TotalsTest extends \Magento\TestFramework\TestCase\AbstractBackendController
16+
{
17+
/** @var LayoutInterface */
18+
protected $_layout;
19+
20+
/** @var Totals */
21+
protected $_block;
22+
23+
/** @var OrderFactory */
24+
private $orderFactory;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp()
30+
{
31+
parent::setUp();
32+
$this->_layout = $this->_objectManager->get(LayoutInterface::class);
33+
$this->_block = $this->_layout->createBlock(Totals::class, 'totals_block');
34+
$this->orderFactory = $this->_objectManager->get(OrderFactory::class);
35+
}
36+
37+
/**
38+
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon.php
39+
*/
40+
public function testShowShippingCoupon()
41+
{
42+
/** @var Order $order */
43+
$order = $this->orderFactory->create();
44+
$order->loadByIncrementId('100000001');
45+
46+
$this->_block->setOrder($order);
47+
$this->_block->toHtml();
48+
49+
$shippingTotal = $this->_block->getTotal('shipping');
50+
$this->assertNotFalse($shippingTotal, 'Shipping method is absent on the total\'s block.');
51+
$this->assertContains('1234567890', $shippingTotal->getLabel(), 'Coupon code is absent in the shipping method label name.');
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Sales\Api\OrderRepositoryInterface;
8+
use Magento\Sales\Model\Order;
9+
use Magento\Sales\Model\Order\Item as OrderItem;
10+
11+
require __DIR__ . '/../../../Magento/Sales/_files/order.php';
12+
/** @var \Magento\Catalog\Model\Product $product */
13+
14+
/** @var OrderItem $orderItem */
15+
$orderItem = $objectManager->create(OrderItem::class);
16+
$orderItem->setProductId($product->getId())
17+
->setQtyOrdered(2)
18+
->setBasePrice($product->getPrice())
19+
->setPrice($product->getPrice())
20+
->setRowTotal($product->getPrice())
21+
->setProductType('simple')
22+
->setName($product->getName())
23+
->setFreeShipping('1');
24+
25+
/** @var Order $order */
26+
$order->setShippingDescription('Flat Rate - Fixed')
27+
->setShippingAmount(0)
28+
->setCouponCode('1234567890')
29+
->setDiscountDescription('1234567890')
30+
->addItem($orderItem);
31+
32+
/** @var OrderRepositoryInterface $orderRepository */
33+
$orderRepository = $objectManager->create(OrderRepositoryInterface::class);
34+
$orderRepository->save($order);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
require 'default_rollback.php';

0 commit comments

Comments
 (0)