在 Woocommerce 中有条件地禁用下订单按钮

Disable Place Order button conditionally in Woocommerce

我在一个函数中有一些代码,当自定义字段的总附加值超过特定值时,它会向最终用户显示一条消息。基本上是一条消息,告诉客户他们订购的家具太多,装不下 68 立方米的集装箱。

我也有代码假设禁用产品上的添加到购物车按钮 - 这似乎不起作用。

所以当我的自定义字段总值超过 68 时,下面的代码中是否存在一个明显的问题来禁用添加到购物车按钮,并且有没有办法完全禁用提交订单,直到无法提交订单自定义字段总数的值低于 68?

显示消息的代码如下,工作正常。

  // display message when cart volume exceeds 68mcube
add_action('woocommerce_before_calculate_totals', 'display_custom_notice', 
50, 1);
function display_custom_notice( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
    return;

 $total_volume = 0;

 // Loop through cart items and calculate total volume
 foreach( WC()->cart->get_cart() as $cart_item ){
    $product_volume = (float) get_post_meta( $cart_item['product_id'], 
'_item_volume', true );
    $total_volume  += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 68 && $total_volume != 0 ){
    // Display a custom notice
    wc_add_notice( __("Note: Your order total volume has reached a full 40ft 
 container - 68 m3 - Please submit order with volume no greater than 68m3", 
"woocommerce"), 'notice' );
 }
 }

下面的功能似乎不起作用

// function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
$total_volume = 0;

 // Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
    $product_volume = (float) get_post_meta( $cart_item['product_id'], 
 '_item_volume', true );
    $total_volume  += $product_volume * $cart_item['quantity'];
 }
 return $total_volume;
 }

 // Replacing the button add to cart by a link to the product in Shop and 
 archives pages
 add_filter( 'woocommerce_loop_add_to_cart_link', 
'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {

if( get_total_volume() > 68 ){
    $button_text = __( "View product", "woocommerce" );
    $button = '<a class="button" href="' . $product->get_permalink() . '">' 
 . $button_text . '</a>';
 }

 return $button;
 }

add_action( 'woocommerce_single_product_summary', 
'remove_add_to_cart_button', 1 );
 function remove_add_to_cart_button() {
// Only when total volume is up to 68
if( get_total_volume() <= 68 ) return;

global $product;

// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
    remove_action( 'woocommerce_single_variation', 
'woocommerce_single_variation_add_to_cart_button', 20 );
    add_action( 'woocommerce_single_product_summary', 
'innactive_add_to_cart_button', 20 );
}
// For all other product types
else {
    remove_action( 'woocommerce_single_product_summary', 
'woocommerce_template_single_add_to_cart', 30 );
    add_action( 'woocommerce_single_product_summary', 
'innactive_add_to_cart_button', 30 );
}
}

要在总购物车体积超过 68 立方米时替换结帐页面中的 "Place Order" 按钮,我们需要在以下代码中使用实用函数 get_total_volume()

// Utility function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
    $total_volume = 0;

     // Loop through cart items and calculate total volume
    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
        $total_volume  += $product_volume * $cart_item['quantity'];
    }
    return $total_volume;
}

// Replacing the Place order button when total volume exceed 68 m3
add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {
    // Only when total volume is up to 68
    if( get_total_volume() <= 68 ) return $order_button;

    $order_button_text = __( "Max volume reached", "woocommerce" );

    $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
    return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

当总体积超过 68 立方米时,您将获得一个无效的自定义按钮:

Check that the utility function get_total_volume() is defined only once, to avoid errors…


现在,要禁用添加到购物车按钮,您的代码中缺少一些东西,例如函数 inactive_add_to_cart_button() 和其他小东西……试试这个:

// display message when cart volume exceeds 68mcube
add_action('woocommerce_before_calculate_totals', 'display_custom_notice', 50, 1);
function display_custom_notice( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

     $total_volume = 0;

     // Loop through cart items and calculate total volume
     foreach( WC()->cart->get_cart() as $cart_item ){
        $product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
        $total_volume  += $product_volume * $cart_item['quantity'];
    }
    if( $total_volume > 68 && $total_volume != 0 ){
        // Display a custom notice
        wc_clear_notices();
        wc_add_notice( __("Note: Your order total volume has reached a full 40ft container - 68 m3 - Please submit order with volume no greater than 68m3",  "woocommerce"), 'notice' );
     }
 }

// Utility function to disable add to cart when volume exceeds 68m3
function get_total_volume(){
    $total_volume = 0;

     // Loop through cart items and calculate total volume
    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
        $total_volume  += $product_volume * $cart_item['quantity'];
    }
    return $total_volume;
}

// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Only when total volume is up to 68
    if( get_total_volume() <= 68 ) return $button;

    $small_text = '<br><em style="font-size:85%;">(' . __( "Max volume reached", "woocommerce" ) . ')</em>';
    $button_text = __( "View product", "woocommerce" ) . $small_text;
    return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

// Replacing the button add to cart by an inactive button on single product pages
add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
function remove_add_to_cart_button() {
    // Only when total volume is up to 68
    if( get_total_volume() <= 68 ) return;

    global $product;

    // For variable product types (keeping attribute select fields)
    if( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        add_action( 'woocommerce_single_product_summary', 'inactive_add_to_cart_button', 20 );
    }
    // For all other product types
    else {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_single_product_summary', 'inactive_add_to_cart_button', 30 );
    }
}

// Utility function: displays a custom innactive add to cart button replacement
function inactive_add_to_cart_button(){
    global $product;

    $style = 'style="color:#fff;cursor:not-allowed;background-color:#999;"';
    echo '<a class="button" '.$style.'>' . __ ( 'Max volume reached', 'woocommerce' ) . '</a>';
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

Check that the utility function get_total_volume() is defined only once, to avoid errors…


当总购物车容积达到 68m3 时,您将获得:

1) 在商店和档案页面上:

2) 在单个产品页面上: