在 WooCommerce 更新结帐事件上动态更新自定义内容

问题描述 投票:0回答:1

class CheckoutTax
{
    public function __construct()
    {
        // add a note after the order details, when a tax is charged and country changes 
        
        // the html snippet is shown after the order details on a page refresh
        add_action('woocommerce_review_order_before_payment', array($this, 'checkout_note_tax'));

        // the action is called when the country changes
        // but it does not show the html snippet at the correct place
        add_action('woocommerce_cart_calculate_fees', array($this, 'checkout_note_tax'));

        // how to combine
        // - show HTML snippet after the order details
        // - act on a country change
    }

    /**
     * Show checkout note, tax paid
     *
     * @return void
     */
    function checkout_note_tax() {
        
        if (is_admin() && !defined('DOING_AJAX')) return;

        if (!is_checkout()) return;

        global $WOOCS;

        $total_tax = WC()->cart->get_totals()['total_tax'];
        $shipping_country = WC()->customer->get_shipping_country();

        error_log( $total_tax );
        error_log( $shipping_country );

        if( $total_tax > 0 && $shipping_country != 'CH') {
            ?>
            <h3>VAT / Custom Duties</h3>
            <?php
        }
    }
}

挂钩 woocommerce_review_order_before_ payment 在正确的页面显示 html 片段,但仅在页面刷新时显示。

挂钩 woocommerce_cart_calculate_fees 触发对国家/地区字段所做的更改。

如何组合操作以在结帐页面上的订单详细信息后显示 HTML 片段,并在运送国家/地区更改时更新 HTML 片段?

php ajax woocommerce hook-woocommerce checkout
1个回答
0
投票

您需要一些不同的东西才能在

checkout_update
Ajax 事件上刷新您的自定义内容。我已将
woocommerce_review_order_before_payment
挂钩替换为更方便的挂钩。

现在您的内容也被添加到 Ajax“订单审核片段”中,因此在“更新结帐”事件中您的显示会动态刷新。

尝试以下(评论)

class CheckoutTax
{

    public function __construct()
    {
        add_action( 'woocommerce_checkout_order_review', array( $this, 'checkout_display_custom_taxes'), 15 );
        add_filter( 'woocommerce_update_order_review_fragments', array( $this, 'update_checkout_custom_taxes_fragments') );
    }

    /**
     * Here your HTML content
     *
     * @return string
     */   
    public function output() {
        $total_tax = WC()->cart->get_totals()['total_tax'];
        $shipping_country = WC()->customer->get_shipping_country();
    
        $html = '<div class="woocommerce-checkout-custom-tax">'; // Mandatory container html
    
        if( $total_tax > 0 && $shipping_country !== 'CH') {
            $html .= sprintf('<h3>%s</h3>', __('VAT / Custom Duties', 'woocommerce') );
        }
        return $html . '</div>';
    }

    /**
     * Output html content in checkout page
     *
     * @return void
     */   
    public function checkout_display_custom_taxes() {
        echo $this->output();
    }

    /**
     * Refresh the html content on checkout update Ajax event
     *
     * @return array
     */   
    public function update_checkout_custom_taxes_fragments( $fragments ){
        $fragments['.woocommerce-checkout-custom-tax'] = $this->output();
    
        return $fragments;
    }
}

为了在子主题的functions.php 文件中进行测试,我另外使用:

add_action('woocommerce_checkout_init', function(){
    $CheckoutTax = new CheckoutTax();
});

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.