简体   繁体   中英

Hide "Add to cart" button when the product price is zero

I have an event based WordPress website on that I sell tickets using WooCommerce. Is there any way to hide the "add to cart" button for the product having cost zero?

Thanks.

You write this code in your theme function.php

function remove_add_to_cart_on_0 ( $purchasable, $product ){
        if( $product->get_price() == 0 )
            $purchasable = false;
        return $purchasable;
    }
    add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_on_0', 10, 2 );

This code work like charm in function.php using this two filter.

add_filter('remove_add_to_cart', 'my_woocommerce_is_purchasable', 10, 2);
function remove_add_to_cart($is_purchasable, $product) {
        if( $product->get_price() == 0 )
            $is_purchasable = false;
            return $purchasable;   
}


function remove_add_to_cart_on_0 ( $purchasable, $product ){
        if( $product->get_price() == 0 )
            $purchasable = false;
        return $purchasable;
    }
add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_on_0', 10, 2 );

This may be a suitable solution according to the documentation https://woocommerce.com/document/conditional-tags/

function remove_add_to_cart_on_0 ( $is_purchasable, $product ){
    if( $product->get_price() == 0 ) {
        return false; }
    else {
        return $is_purchasable; }
}
add_filter( 'woocommerce_is_purchasable', 'remove_add_to_cart_on_0', 10, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM