Skip to content

add coupon code to totals model #32797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/code/Magento/Sales/Block/Order/Totals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Sales\Block\Order;

use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @SilinMykola
Thank you for your contribution;
Please don't forget to keep an empty string after imports

Suggested change
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Model\Order\Invoice;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @andrewbess
Thanks for your reviewing, I fixed your remark.


/**
* Order totals.
Expand Down Expand Up @@ -126,16 +127,21 @@ protected function _initTotals()
* Add discount
*/
if ((double)$this->getSource()->getDiscountAmount() != 0) {
if ($this->getSource()->getDiscountDescription()) {
$discountLabel = __('Discount (%1)', $source->getDiscountDescription());
if ($this->getSource() instanceof Invoice) {
$discountSource = $this->getSource()->getOrder();
} else {
$discountSource = $this->getSource();
}
if ($this->getSource()->getCouponCode()) {
$discountLabel = __('Discount (%1)', $discountSource->getCouponCode());
Comment on lines +130 to +136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I see from the suggested solution, the coupon code description will not be shown anymore, but it was expected to be shown. In other words, this solution replaces coupon code description with its code. It is not correct from the user experience.
It is also impossible to change the $discountLabel text because this class is an @api (BIC change).

I can suggest to implement something like this:

            $discountLabel = $this->getSource()->getDiscountDescription()
                ? __('Discount (%1)', $source->getDiscountDescription())
                : __('Discount (%1)', $source->getCouponCode());

With this, we will have a coupon code or description in the Discount line. And not the BIC change.

} else {
$discountLabel = __('Discount');
}
$this->_totals['discount'] = new \Magento\Framework\DataObject(
[
'code' => 'discount',
'field' => 'discount_amount',
'value' => $source->getDiscountAmount(),
'value' => $discountSource->getDiscountAmount(),
'label' => $discountLabel,
]
);
Expand Down