Php Magento 2在管理发票中添加额外费用

Php Magento 2在管理发票中添加额外费用,php,magento2,Php,Magento2,我已经创建了一个模块,用于向订单中添加额外费用。在前面,当我下订单时,它成功地在前面添加了额外的费用。但在管理员方面并没有增加任何额外费用。请帮助我在哪里我做了错误添加额外的费用到管理发票。 我在模型中创建了collect方法。。代码如下所示 namespace Mageniks\Alltest\Model\Total; class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal { protect

我已经创建了一个模块,用于向订单中添加额外费用。在前面,当我下订单时,它成功地在前面添加了额外的费用。但在管理员方面并没有增加任何额外费用。请帮助我在哪里我做了错误添加额外的费用到管理发票。 我在模型中创建了collect方法。。代码如下所示

namespace Mageniks\Alltest\Model\Total;

class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{

    protected $quoteValidator = null; 
     protected $_checkoutSession;
    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Checkout\Model\Session $checkoutSession)
    {
        $this->quoteValidator = $quoteValidator;
         $this->_checkoutSession = $checkoutSession;
    }
  public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);


        $exist_amount = 0; //$quote->getFee(); 
        $giftprice = $this->_checkoutSession->getGiftwrapprice();
        $fee = $giftprice; 
        $balance = $fee - $exist_amount;

        $total->setTotalAmount('fee', $balance);
        $total->setBaseTotalAmount('fee', $balance);

        $total->setFee($balance);
        $total->setBaseFee($balance);

        $total->setGrandTotal($total->getGrandTotal() + $balance);
        $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);


        return $this;
    } 

    protected function clearValues(Address\Total $total)
    {
        $total->setTotalAmount('subtotal', 0);
        $total->setBaseTotalAmount('subtotal', 0);
        $total->setTotalAmount('tax', 0);
        $total->setBaseTotalAmount('tax', 0);
        $total->setTotalAmount('discount_tax_compensation', 0);
        $total->setBaseTotalAmount('discount_tax_compensation', 0);
        $total->setTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setSubtotalInclTax(0);
        $total->setBaseSubtotalInclTax(0);
    }

    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $giftprice = $this->_checkoutSession->getGiftwrapprice();
        return [
            'code' => 'fee',
            'title' => 'Giftwrap',
            'value' => $giftprice
        ];
    }

    public function getLabel()
    {
        return __('Giftwrap');
    }
}
我也在关注这个链接: 但它不能在管理发票中添加额外费用

任何帮助都将不胜感激。
感谢将额外费用添加到管理员发票中,请按照以下步骤操作:

在app/code/Namespace/Modulename/view/adminhtml/layout/sales\u order\u invoice\u new.Xml创建Xml文件

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

        <body>        
            <referenceContainer name="invoice_totals">
                <block class="Namespace\Module\Block\Adminhtml\Sales\Order\Invoice\Extrafee" name="extrafee"/>
            </referenceContainer>
        </body>
    </page>

在app/code/Namespace/Module/Block/Adminhtml/Sales/Order/Invoice/Extrafee.php创建文件

    <?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    /**
     * Tax totals modification block. Can be used just as subblock of \Magento\Sales\Block\Order\Totals
     */
    namespace Namespace\Module\Block\Adminhtml\Sales\Order\Invoice;



    class Extrafee extends \Magento\Framework\View\Element\Template
    {
        protected $_config;
        protected $_order;
        protected $_source;

        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Tax\Model\Config $taxConfig,
            array $data = []
        ) {
            $this->_config = $taxConfig;
            parent::__construct($context, $data);
        }

        public function displayFullSummary()
        {
            return true;
        }

        public function getSource()
        {
            return $this->_source;
        } 
        public function getStore()
        {
            return $this->_order->getStore();
        }
        public function getOrder()
        {
            return $this->_order;
        }
        public function getLabelProperties()
        {
            return $this->getParentBlock()->getLabelProperties();
        }

        public function getValueProperties()
        {
            return $this->getParentBlock()->getValueProperties();
        }
         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' => $this->_order->getFee(),
                            'base_value' => $this->_order->getFee(),
                            'label' => __('Fee'),
                        ]
                    );
                    $parent->addTotal($fee, 'fee');
                    return $this;

        }

    }

我有一个问题:在发票之前的Magento 2管理中,订单总计没有看到任何额外费用行,但当我创建发票时,会自动生成额外费用行。那么,当订单状态为“待定”或在发票生成之前添加额外费用的方法是什么呢?在评论中添加答案是相当困难的。所以,请为这个问题创建另一个问题,所以我肯定会给你一个答案。好的。。。但是我得到了答案。。我必须创建sales\u order\u view.xml和与sales\u order\u invoice\u new.xml相同的引用名称集。我说得对吗?是的,你完全正确。并创建类似invoice&define on xml的类。只需复制发票代码,不需要更改代码中的任何内容,只需更改类名即可。效果很好,但更改显示总计的正确方法是什么??我通过执行
$this->getInvoice()->setGrandTotal($this->getInvoice()->getGrandTotal()+$this->getOrder()->getfeeaumount())解决了这个问题不会因为在“发票视图”中添加额外费用而弄乱“发票视图”