0

The version of my magento2 is 2.3.5-p1.

Here is the problem: We will open the invoice view in backend. So we go to: ->Sales->orders->select an order ->go to Invoices->view

enter image description here

enter image description here

Does someone has the same issue and were able to fix it? Any ideas how to fix it?

I am happy for any ideas!

Thanks for helping.

Totals.php enter image description here

public function afterGetTotals(\Magento\Sales\Block\Order\Totals $subject, ?array $totals)
{
    if (!is_array($totals)) {
        return null;
    }
    foreach ($totals as $total) {
        if ($total->getCode() == 'discount') {
            if ($this->_coreRegistry->registry('current_order')->getAppliedRuleIds() == self::POS_CUSTOM_DISCOUNT
                && $this->_coreRegistry->registry('current_order')->getOsPosCustomDiscountAmount()) {
                $label = "Custom Discount";
                if ($this->_coreRegistry->registry('current_order')->getOsPosCustomDiscountType()
                    == self::PERCENT_PRICE_TYPE) {
                    $label .= sprintf(
                        " (%.2f%%)",
                        $this->_coreRegistry->registry('current_order')->getOsPosCustomDiscountAmount()
                    );
                }
                $total->setLabel($label);
            }
        }
    }
    return $totals;
}
5
  • Give the code of Totals.php
    – Harun
    Mar 29 at 6:29
  • I can't find a place to upload files.
    – Alex
    Mar 29 at 6:38
  • You don't need to upload files just copy and paste that particular code.
    – Harun
    Mar 29 at 6:42
  • I edited the question, the code and screenshots can be seen in the question, Thank you!
    – Alex
    Mar 29 at 6:45
  • I found these orders have the same feature, they are all using coupons.
    – Alex
    Mar 29 at 6:48

1 Answer 1

0

This line is checking whether the AppliedRuleIds value of the current order object is equal to self::POS_CUSTOM_DISCOUNT, and whether the OsPosCustomDiscountAmount value of the current order object is not empty.

If the current_order object is null or if the AppliedRuleIds value or OsPosCustomDiscountAmount value are not defined, then the code could result in a fatal error due to a null pointer exception.

To prevent the null pointer exception, you can add null checks to ensure that the current_order object is not null and that the values you are trying to access are defined before accessing them.

Use this code on line 29,

$currentOrder = $this->_coreRegistry->registry('current_order');
if ($currentOrder && $currentOrder->getAppliedRuleIds() == self::POS_CUSTOM_DISCOUNT
                && $currentOrder->getOsPosCustomDiscountAmount()) {
2
  • This is a great answer,it solved my problem perfectly, thank you so much.
    – Alex
    Mar 29 at 7:08
  • You're very welcome! I'm happy to help
    – Harun
    Mar 29 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.