3

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Taniya Silhi
  • 84
  • 1
  • 8

3 Answers3

4

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 );
SNS
  • 485
  • 6
  • 20
0

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 );
Arpan Sagar
  • 166
  • 8
  • The second function is doing all the work. The first function can be removed and has an incorrect order for the parameters of the add_filter function. Also "my_woocommerce_is_purchasable" is not a valid filter name. – Jordan Carter Oct 21 '21 at 18:08
0

This may be a suitable solution according to the documentation

woocommerce_is_purchasable – filter Function name: WC_Product::is_purchasable is_purchasable() — Returns false if the product cannot be bought. https://woocommerce.wp-a2z.org/oik_api/wc_productis_purchasable/

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 );
Р. Р.
  • 75
  • 7
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30903050) – paneerakbari Jan 28 '22 at 14:13