首页 > 解决方案 > 在前端结帐/管理订单视图中更新订单总数 - M2

问题描述

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

我已经尝试通过自定义 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 更改订单总额。在这里,我成功地在前端和管理订单视图上进行了更新,但是只有在验证了增值税字段之后,我才能设法进行这些调整。这是我的模型收集功能:

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" ){ 
               .........
}

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

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

提前致谢

标签: magentomagento2checkout

解决方案


推荐阅读