【问题标题】:Disable Add to cart button when cart calculated volume limit is reached in woocommerce在 woocommerce 中达到购物车计算的数量限制时禁用添加到购物车按钮
【发布时间】:2018-09-20 02:12:53
【问题描述】:

当自定义字段的组合值达到指定值时,是否有一种技术方法可以禁用 woocommerce 中的添加到购物车按钮。

我正在使用下面的代码计算自定义字段_item_volume的总值,并希望在值达到68时禁用添加到购物车按钮功能。

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 68 m3", 
    "woocommerce"), 'notice' );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart volume


    【解决方案1】:

    尝试以下方法:

    // Utility function
    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 );
        }
    }
    
    
    
    // Utility function: displays a custom innactive add to cart button replacement
    function innactive_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 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 2018-05-08
      • 2018-09-30
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      相关资源
      最近更新 更多