在前端结帐/管理员订单视图中更新订单总数-M2

时间:2019-04-15 14:21:53

标签: magento magento2 checkout

在结帐页面上,我需要在验证VAT字段(验证在控制器中完成)之后更新订单总计并从总计中删除VAT。价格也需要保存在后端的订单视图中。

我已经尝试通过自定义js进行更新,并且在前端工作正常,但是无法保存在后端的订单视图中

define([
    'jquery',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/resource-url-manager',
    'mage/storage',
    'Magento_Checkout/js/action/get-payment-information',
    'Magento_Checkout/js/model/totals',
    'Magento_Checkout/js/model/full-screen-loader',

    'Magento_Checkout/js/action/get-totals',
    'Magento_Checkout/js/model/cart/totals-processor/default'
], function ($, quote, urlManager,storage, getPaymentInformationAction, totals, fullscreenLoader, 
    getTotalsAction) {
    'use strict';

    return function (removeTax, tax) {
        var payload = {
            addressInformation: {
                shipping_address: quote.shippingAddress(),
                billing_address: quote.billingAddress(),
                shipping_method_code: quote.shippingMethod().method_code,
                shipping_carrier_code: quote.shippingMethod().carrier_code
                }
            };

        fullscreenLoader.startLoader();

        return storage.post(
            urlManager.getUrlForSetShippingInformation(quote),
            JSON.stringify(payload)
        ).done(function (response) {

            if(removeTax) {
                response.totals.total_segments[2].value = -tax;
                response.totals.total_segments[4].value -= tax;
            } else {
                response.totals.total_segments[2].value = tax;
                response.totals.total_segments[4].value += tax;
            }

            var deferred = $.Deferred();
            totals.isLoading(true);

            getTotalsAction([], deferred);

            $.when(deferred).done(function () {
                quote.setTotals(response.totals);
                totals.isLoading(false);
                fullscreenLoader.stopLoader();
            });


        }).fail(function (response) {
            console.log('fail');
            totals.isLoading(false);
            fullScreenLoader.stopLoader();            
        });

    };
});

另一方面,我也尝试通过etc / sales.xml更改订单总数。在这里,我成功地在前端视图和管理订单视图上进行了更新,但是仅在验证了VAT字段之后,我才能设法进行这些调整。这是我的模型收集功能:

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);
                $currentTax = $total->getTaxAmount();

                $total->setTaxAmount(-$currentTax);
                $total->setBaseTaxAmount(-$currentTax);

                $total->setGrandTotal($total->getGrandTotal() - $currentTax);
                $total->setBaseGrandTotal($total->getBaseGrandTotal() - $currentTax);


        return $this;
    }

因此,我尝试设置如下会话消息:$ this-> session-> setMySession(“ Valid VAT”);在我要验证增值税的控制器中,然后仅在以下情况下在模型中进行更改:

if($this->session->getMySession() == "Valid VAT" ){ 
               .........
}

但是它没有设置会话消息,因为模型是在第一次加载页面时加载一次,并且在验证之后我无法重新加载它。

只有在控制器中进行验证之后,任何人都可以帮助或指导我如何更改价格吗?

预先感谢

0 个答案:

没有答案