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

时间:2018-09-12 10:20:54

标签: php wordpress woocommerce cart shipping-method

这看起来应该很简单,但我不太明白。

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

我在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

现在使用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英镑的订单的付款/付款,并告知客户原因。

1 个答案:

答案 0 :(得分: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