在Woocommerce中有条件地禁用“下订单”按钮 [英] Disable Place Order button conditionally in Woocommerce

查看:88
本文介绍了在Woocommerce中有条件地禁用“下订单”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数中有一些代码,当自定义字段的总添加值超过某个值时,该消息会向最终用户显示一条消息。基本上,这是一条消息,告诉客户他们订购了太多家具,无法放入68m3的容器中。

I have some code in a function that displays a message to the end user when the total added value of a custom field exceeds a certain value. Basically its a message telling the customer that they have ordered too much furniture to fit into a 68m3 container.

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

I also have code which is suppose to disable the add to cart button on products - this doesn't seem to work.

下面的代码中是否有一个可见的问题,当我的自定义字段总值超过时,禁用了添加到购物车按钮68,并且有一种方法可以完全禁用提交订单,因此只有在自定义字段的总值低于68之前,才能提交订单?

So is there a visible issue in the code below to disable the add to cart button when my custom field total value exceeds 68 and is there a way to totally disable the submit order so it's not possible to submit the order until the value of the custom field total is below 68?

显示消息如下,并且工作正常。

The code to display the message is as follows and works fine.

  // 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立方米时,请在结帐页面中替换下订单按钮,我们将需要在以下代码中使用实用函数 get_total_volume()

To replace "Place Order" button in checkout page when total cart volume exceed 68 m3 we will need to use your utility function get_total_volume() in the following code:

// 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文件)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

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

When total volume exceed 68 m3, you will get an inactive custom button instead:


检查实用程序函数 get_total_volume()仅定义一次,以避免错误……

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






现在,要禁用添加到购物车按钮,代码中会丢失一些东西,例如函数 inactive_add_to_cart_button()和其他一些小东西……尝试以下操作:


Now, to disable add to cart buttons, there is some missing things in your code like the function inactive_add_to_cart_button() and other little things… Try this:

// 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文件)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


检查是否仅定义了实用函数 get_total_volume()一次,为了避免错误…

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






当购物车的总体积达到68m3时,将会得到:


When the total cart volume is up to 68m3 you will get:

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

1) On shop and archives pages:

2)在单个产品页面上:

2) On single product pages:

这篇关于在Woocommerce中有条件地禁用“下订单”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆