首页 文章

在woocommerce中达到购物车计算的音量限制时,禁用添加到购物车按钮

提问于
浏览
1

当自定义字段的组合值达到指定值时,是否有技术方法可禁用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' );
    }
}

1 回答

  • 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文件中 . 经过测试和工作 .

相关问题