首页 > 解决方案 > 在 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 );
}
}

标签: wordpresswoocommercecartproductvolume

解决方案


要在购物车总体积超过 68 m3 时替换结帐页面中的“下订单”按钮,我们将需要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 m3 时,您将获得一个无效的自定义按钮:

在此处输入图像描述

检查效用函数get_total_volume()是否只定义一次,以避免错误……</p>


现在,要禁用添加到购物车按钮,您的代码中缺少一些东西,比如函数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 文件中。测试和工作。

检查效用函数get_total_volume()是否只定义一次,以避免错误……</p>


当购物车总体积达到 68 立方米时,您将获得:

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

在此处输入图像描述

2) 在单个产品页面上:

在此处输入图像描述


推荐阅读