2

I need to show used coupon code in backend order details page. In my searches i found coupon code does not store with order details.

So I was thinking to save coupon code somewhere just after order placement & then used these values in order details page.

Is there any better way doing this. Please help.

2
  • I'm pretty much sure that coupon code is displayed in admin in totals block. If you are having some customization in there, then you might not be able to see that, but in a default Magento, it is saved with order and is displayed in admin as well.
    – Prateek
    Apr 15, 2016 at 11:13
  • Is there any progress on this as I have same question I also want to show the coupon codes in admin panel Dec 23, 2021 at 18:48

1 Answer 1

0

Vendor/Module/view/adminhtml/layout/sales_order_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>      
        <referenceContainer name="order_totals">
            <block class="Vendor\Module\Block\Adminhtml\Sales\Totals" name="coupon_rules"/>
        </referenceContainer>
    </body>
</page>

Vendor/Module/Block/Adminhtml/Sales/Totals.php

<?php
namespace Vendor\Module\Block\Adminhtml\Sales;
class Totals extends \Magento\Sales\Block\Order\Totals
{
    public function initTotals()
    {
        $order = $this->getParentBlock()->getOrder();
        $salesruleIds = explode(',', $order->getAppliedRuleIds());
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $ruleNames = array();
        foreach ($salesruleIds as $key => $ruleId) { 
            $rule=$objectManager->create('Magento\SalesRule\Model\Rule')->load($ruleId); 
            $ruleNames[] = $rule->getName();
        }
        $rules = implode(",",$ruleNames);
        $total = new \Magento\Framework\DataObject(
            [
                'code' => 'coupon_rules',
                'field' => 'coupon_rules',
                'value' => abs($order->getDiscountAmount()),
                'base_value'=> abs($order->getDiscountAmount()),
                'label' => __($rules)
            ]
        );
        $parent = $this->getParentBlock();
        $parent->addTotal($total,'subtotal');
        return $this;
    }
}

Your Answer

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

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