基于运输方式和购物车总数的动态信息在Woocommerce中

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

这似乎应该很简单,但我不能完全达到目的。

目标:如果购物车总数小于所需限额,我需要在用户更改运输方式时向用户显示通知。

我在functions.php文件中写了一个函数

function min_delivery(){

    //Get Chosen Shipping Method//
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; 
    //end method


    //check if they've chosen delivery
    if ($chosen_shipping == 'flat_rate:2') {

        //set cart minimum
        $minimum = 15.00;
        //get cart total - 2.00 is the delivery fee
        $total = WC()->cart->total-2.00;
        // check if the total of the cart is less than the minium
        if ( $total < $minimum ) {
            //check if it's in the cart
            if( is_cart() ) {

                   wc_print_notice( 
                sprintf( 'Minimum £15.00 for Delivery' , 
                    wc_price( $minimum ), 
                    wc_price( $total )
                ), 'error' 
            );
            //else at checkout
            } else {

                wc_add_notice( 
                    sprintf( 'Min 15.00 for Delivery' , 
                        wc_price( $minimum ), 
                        wc_price( $total )
                    ), 'error' 
                );
            }

            }//end of total less than min

    //else they have pick up
    } else {
        //set cart minimum
        $minimum = 4.50;
        //get cart total - 2.00 is the delivery fee
        $total = WC()->cart->total-2.00;
        if ( $total < $minimum ) {
            //check if it's in the cart
            if( is_cart() ) {

                   wc_print_notice( 
                sprintf( 'Minimum £15.00 for Delivery' , 
                    wc_price( $minimum ), 
                    wc_price( $total )
                ), 'error' 
            );
            //else at checkout
            } else {

                wc_add_notice( 
                    sprintf( 'Min 15.00 for Delivery' , 
                        wc_price( $minimum ), 
                        wc_price( $total )
                    ), 'error' 
                );
            }

            }//end of total less than min
    }//end pickup/delivery else


}//end function

现在使用before checkout表单调用此函数效果很好

add_action( 'woocommerce_before_checkout_form' , 'min_delivery' );

然而,我的问题是当人们即时更新航运时,即在他们之间进行选择。我想我会使用一个钩子,比如其中一个

//add_action( 'woocommerce_checkout_process', 'min_delivery' );
//add_action( 'update_order_review' , 'min_delivery' );
//add_action( 'woocommerce_review_order_after_shipping' , 'min_delivery' );
add_action( 'woocommerce_before_checkout_form' , 'min_delivery' );
//add_action( 'woocommerce_after_checkout_shipping_form', 'min_delivery' );
//add_action('woocommerce_cart_totals_after_shipping', 'min_delivery');  

但是他们都没有完成这项工作。当我查看控制台时,更新时有一个对?wc-ajax=update_order_review的AJAX调用,但我似乎无法挂钩

如果我在这里咆哮错误的树,我会徘徊,你甚至可以通过PHP已经渲染的钩子和函数来修改页面?

根据目标,重点是限制购物总额低于15英镑的订单/付款,并通知客户原因。

php wordpress woocommerce cart shipping-method
1个回答
0
投票

我重新访问并尝试简化您的代码。以下内容将在发货后有条件地添加购物车和结帐总计表中的自定义信息消息(动态刷新):

add_action( 'woocommerce_cart_totals_after_shipping', 'shipping_notice_displayed', 20 );
add_action( 'woocommerce_review_order_after_shipping', 'shipping_notice_displayed', 20 );
function shipping_notice_displayed() {
    if ( did_action( 'woocommerce_cart_totals_after_shipping' ) >= 2 ||
        did_action( 'woocommerce_review_order_after_shipping' ) >= 2 ) {
        return;
    }

    // Chosen Shipping Method
    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
    $cart_subtotal             = WC()->cart->subtotal; // No need to remove the fee

    // Settings
    $cart_minimum1             = 4.5;
    $cart_minimum2             = 15;

    // HERE define the cart mininimum (When delivery is chosen)
    if ( $cart_subtotal < $cart_minimum2 && $chosen_shipping_method != 'flat_rate' ) {
        $cart_minimum = $cart_minimum1;
    } elseif ( $cart_subtotal < $cart_minimum2 && $chosen_shipping_method == 'flat_rate' ) {
        $cart_minimum = $cart_minimum2;
    }

    // Display a message
    if( isset($cart_minimum) ){
        // The message
        $message =  sprintf( '%s %s for Delivery' ,
            is_cart() ? 'Minimum' : 'Min',
            strip_tags( wc_price( $cart_minimum, array('decimals' => 2 ) ) )
        );

        // Display
        echo '</tr><tr class="shipping info"><th>&nbsp;</th><td data-title="Delivery info">'.$message.'</td>';
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here


推荐问答