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

现在使用结帐前表单调用此函数效果很好

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');  

然而,他们都没有做这项工作。当我查看控制台时,更新时有一个 AJAX 调用,?wc-ajax=update_order_review但我似乎无法与之挂钩

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

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

4

1 回答 1

1

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

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 文件中。测试和工作。

在此处输入图像描述

于 2018-09-12T18:01:29.133 回答