1

I created custom fee based on following link.

(Please look at my module at : https://github.com/sivajik34/Delivery-Signature-Magento2)

how to add fee to order totals in magento2

Everything is working fine.but in order view page I'm getting following issue.

enter image description here

i think issue with following function

 /**
     * Initialize all order totals relates with tax
     *
     * @return \Magento\Tax\Block\Sales\Order\Tax
     */
    public function initTotals()
    {

        $parent = $this->getParentBlock();
        $this->_order = $parent->getOrder();
        $this->_source = $parent->getSource();

        $store = $this->getStore();

        $fee = new \Magento\Framework\DataObject(
            [
                'code' => 'fee',
                'strong' => false,
                //'value' => 1090,
                'value' => $this->_source->getFee(),
                'label' => __('Fee'),
            ]
        );

        $parent->addTotal($fee, 'fee');
        // $this->_addTax('grand_total');
        $parent->addTotal($fee, 'fee');


        return $this;
    }

update: InstallData.php

<?php

namespace Kensium\DeliverySign\Setup;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Sales\Setup\SalesSetupFactory;


/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * Sales setup factory
     *
     * @var SalesSetupFactory
     */
    protected $salesSetupFactory;

    public function __construct(
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->salesSetupFactory = $salesSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
        $options = ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, 'required' => false];
        $salesSetup->addAttribute('order', 'fee', $options);
        $salesSetup->addAttribute('order', 'base_fee', $options);

    }
}

fieldset.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_quote_address">
            <field name="fee">
                <aspect name="to_order" />
            </field>
            <field name="base_fee">
                <aspect name="to_order" />
            </field>
            </fieldset>
        </scope>
    </config>

Order fields added in sales_order table.but still values are not saved.

Sivakumar K
  • 2,013
  • 9
  • 27
  • 43

1 Answers1

0

First of all you should change your Installer to directly adding DB columns to both the 'quote' and the 'sales_order' DB table. The 'sales_order_grid' might be nice to have too.

Secondly I suggest using an observer instead for transfering your data from quote to order:

add this to your etc/events.xml

    <?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_before">
        <observer name="your_obeserver_name" instance="Namespace\Module\Model\Observer\SaveFeeToOrderObserver"/>
    </event>
</config>

After that you can add your Observer to Model\Observer\SaveFeeToOrderObserver.php:

<?php
namespace Namespace\Module\Model\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SaveFeeToOrderObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectmanager
     */
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectmanager)
    {
        $this->_objectManager = $objectmanager;
    }

    public function execute(EventObserver $observer)
    {
        $order = $observer->getOrder();
        $quoteRepository = $this->_objectManager->create('Magento\Quote\Model\QuoteRepository');
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $quoteRepository->get($order->getQuoteId());
        $order->setFee( $quote->getFee() );
        $order->setBaseFee( $quote->getBaseFee() );

        return $this;
    }

}
mybinaryromance
  • 151
  • 1
  • 11