Skip to content

Commit 151f06e

Browse files
author
Jon Doe
committed
ACP2E: Free Shipping does not show in totals on invoice page
1 parent ba0b59c commit 151f06e

File tree

9 files changed

+164
-22
lines changed

9 files changed

+164
-22
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Order\Invoice;
77

8+
use Magento\Framework\DataObject;
89
use Magento\Sales\Model\Order\Invoice;
910

1011
/**

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ protected function _initTotals()
9191
* Add shipping
9292
*/
9393
if (!$order->getIsVirtual()
94-
&& ((double)$order->getShippingAmount()
94+
&& ($order->getShippingAmount() !== null
9595
|| $order->getShippingDescription())
9696
) {
9797
$shippingLabel = __('Shipping & Handling');
9898

99-
if ($order->getCouponCode() && !isset($this->_totals['discount'])) {
100-
$shippingLabel .= " ({$order->getCouponCode()})";
99+
if (!isset($this->_totals['discount'])) {
100+
if ($order->getCouponCode() ) {
101+
$shippingLabel .= " ({$order->getCouponCode()})";
102+
} else if ($order->getDiscountDescription()) {
103+
$shippingLabel .= " ({$order->getDiscountDescription()})";
104+
}
101105
}
102106

103107
$this->_totals['shipping'] = new DataObject(

app/code/Magento/Sales/Block/Order/Invoice/Totals.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Sales\Block\Order\Invoice;
77

8-
use Magento\Sales\Model\Order;
8+
use Magento\Sales\Model\Order\Invoice;
99

1010
/**
1111
* @api
@@ -62,7 +62,7 @@ public function setInvoice($invoice)
6262
/**
6363
* Get totals source object
6464
*
65-
* @return Order
65+
* @return Invoice
6666
*/
6767
public function getSource()
6868
{

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Block\Order;
77

8+
use Magento\Framework\DataObject;
89
use Magento\Sales\Model\Order;
910

1011
/**
@@ -144,18 +145,25 @@ protected function _initTotals()
144145
/**
145146
* Add shipping
146147
*/
147-
if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
148-
$label = __('Shipping & Handling');
149-
if ($this->getSource()->getCouponCode() && !isset($this->_totals['discount'])) {
150-
$label = __('Shipping & Handling (%1)', $this->getSource()->getCouponCode());
151-
}
148+
if (!$source->getIsVirtual()
149+
&& ($source->getShippingAmount() !== null
150+
|| $source->getShippingDescription())
151+
) {
152+
$shippingLabel = __('Shipping & Handling');
152153

153-
$this->_totals['shipping'] = new \Magento\Framework\DataObject(
154+
if (!isset($this->_totals['discount'])) {
155+
if ($source->getCouponCode() ) {
156+
$shippingLabel .= " ({$source->getCouponCode()})";
157+
} else if ($source->getDiscountDescription()) {
158+
$shippingLabel .= " ({$source->getDiscountDescription()})";
159+
}
160+
}
161+
$this->_totals['shipping'] = new DataObject(
154162
[
155163
'code' => 'shipping',
156164
'field' => 'shipping_amount',
157-
'value' => $this->getSource()->getShippingAmount(),
158-
'label' => $label,
165+
'value' => $source->getShippingAmount(),
166+
'label' => $shippingLabel,
159167
]
160168
);
161169
}

app/code/Magento/Sales/Model/Order/Invoice/Total/Shipping.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class Shipping extends AbstractTotal
1818
*/
1919
public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
2020
{
21-
$invoice->setShippingAmount(0);
22-
$invoice->setBaseShippingAmount(0);
2321
$orderShippingAmount = $invoice->getOrder()->getShippingAmount();
2422
$baseOrderShippingAmount = $invoice->getOrder()->getBaseShippingAmount();
2523
$shippingInclTax = $invoice->getOrder()->getShippingInclTax();
@@ -29,7 +27,7 @@ public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
2927
* Check shipping amount in previous invoices
3028
*/
3129
foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
32-
if ((double)$previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
30+
if ((double)$previousInvoice->getShippingAmount() !== null && !$previousInvoice->isCanceled()) {
3331
return $this;
3432
}
3533
}

app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Total/ShippingTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected function setUp(): void
4444
public function testCollectWithNoOrZeroPrevInvoice(array $prevInvoicesData, $orderShipping, $expectedShipping)
4545
{
4646
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
47-
$invoice->expects($this->exactly(2))
47+
$invoice->expects($this->once())
4848
->method('setShippingAmount')
49-
->withConsecutive([0], [$expectedShipping]);
49+
->with($expectedShipping);
5050

5151
$this->total->collect($invoice);
5252
}
@@ -63,7 +63,7 @@ public static function collectWithNoOrZeroPrevInvoiceDataProvider()
6363
'expectedShipping' => 10.00,
6464
],
6565
'zero shipping in previous invoices' => [
66-
'prevInvoicesData' => [['shipping_amount' => '0.0000']],
66+
'prevInvoicesData' => [['shipping_amount' => null]],
6767
'orderShipping' => 10.00,
6868
'expectedShipping' => 10.00,
6969
],
@@ -75,9 +75,8 @@ public function testCollectWithPreviousInvoice()
7575
$orderShipping = 10.00;
7676
$prevInvoicesData = [['shipping_amount' => '10.000']];
7777
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
78-
$invoice->expects($this->once())
79-
->method('setShippingAmount')
80-
->with(0);
78+
$invoice->expects($this->never())
79+
->method('setShippingAmount');
8180

8281
$this->total->collect($invoice);
8382
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
namespace Magento\Sales\Block\Adminhtml\Order\Invoice;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\Sales\Model\OrderFactory;
14+
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @magentoAppArea adminhtml
20+
* @magentoDbIsolation enabled
21+
*/
22+
class TotalsTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
private $objectManager;
26+
27+
/** @var Totals */
28+
private $block;
29+
30+
/** @var OrderFactory */
31+
private $orderFactory;
32+
33+
/** @var Registry */
34+
private $registry;
35+
36+
/** @var CollectionFactory */
37+
private $invoiceCollectionFactory;
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
parent::setUp();
45+
46+
$this->objectManager = Bootstrap::getObjectManager();
47+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Totals::class);
48+
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
49+
$this->registry = $this->objectManager->get(Registry::class);
50+
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function tearDown(): void
57+
{
58+
$this->registry->unregister('current_invoice');
59+
60+
parent::tearDown();
61+
}
62+
63+
/**
64+
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon_and_invoice.php
65+
*
66+
* @return void
67+
*/
68+
public function testCollectTotals(): void
69+
{
70+
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
71+
$invoice = $this->invoiceCollectionFactory->create()->setOrderFilter($order)->setPageSize(1)->getFirstItem();
72+
$this->registry->unregister('current_invoice');
73+
$this->registry->register('current_invoice', $invoice);
74+
75+
$this->block->toHtml();
76+
$totals = $this->block->getTotals();
77+
$this->assertArrayHasKey('shipping', $totals);
78+
$this->assertEquals('0.0000', $totals['shipping']['value']);
79+
$this->assertEquals('Shipping & Handling (1234567890)', $totals['shipping']['label']);
80+
}
81+
}

dev/tests/integration/testsuite/Magento/Sales/Block/Order/Invoice/TotalsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ protected function setUp(): void
4444
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
4545
}
4646

47+
48+
/**
49+
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon_and_invoice.php
50+
*
51+
* @return void
52+
*/
53+
public function testCollectTotals(): void
54+
{
55+
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
56+
$invoice = $order->getInvoiceCollection()->getFirstItem();
57+
58+
$this->block->setOrder($order);
59+
$this->block->setInvoice($invoice);
60+
61+
$this->block->toHtml();
62+
$totals = $this->block->getTotals();
63+
$this->assertArrayHasKey('shipping', $totals);
64+
$this->assertEquals('0.0000', $totals['shipping']['base_value']);
65+
$this->assertEquals('Shipping & Handling (1234567890)', $totals['shipping']['label']);
66+
}
4767
/**
4868
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
4969
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
10+
use Magento\Sales\Api\OrderRepositoryInterface;
11+
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Model\Order\Item as OrderItem;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
15+
16+
Resolver::getInstance()->requireDataFixture('Magento/Sales/_files/order_with_free_shipping_by_coupon.php');
17+
18+
$objectManager = Bootstrap::getObjectManager();
19+
/** @var \Magento\Sales\Model\Order $order */
20+
$order = $objectManager->get(OrderInterfaceFactory::class)->create()->loadByIncrementId('100000001');
21+
$orderService = $objectManager->create(
22+
\Magento\Sales\Api\InvoiceManagementInterface::class
23+
);
24+
$invoice = $orderService->prepareInvoice($order);
25+
$invoice->register();
26+
$order = $invoice->getOrder();
27+
$order->setIsInProcess(true);
28+
$transactionSave = $objectManager
29+
->create(\Magento\Framework\DB\Transaction::class);
30+
$transactionSave->addObject($invoice)->addObject($order)->save();
31+

0 commit comments

Comments
 (0)