编程语言
首页 > 编程语言> > php – Magento 2在管理发票中增加额外费用

php – Magento 2在管理发票中增加额外费用

作者:互联网

我创建了一个模块,以便为订单增加额外费用.在我下订单时,在前面成功添加额外费用.但在管理员方面没有增加任何额外费用.请帮助我在哪里错误地向管理发票中添加额外费用? .
我在Model中创建了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');
    }
}

我也是按照这个链接:
https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2
但它不能在管理发票中增加额外费用.

任何帮助,将不胜感激.
谢谢

解决方法:

要在管理发票中添加额外费用,请按照以下步骤操作:

在app / code / Namespace / Modulename / view / adminhtml / layout / sales_order_invoice_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;

        }

    }

我希望它能帮到你:)

标签:php,magento2
来源: https://codeday.me/bug/20190608/1198087.html