4

im using Woocommerce and searching for a way to hide the "Add to cart"-Button on
a single-Product page IF the product is for free - I'm making a big CSV-Import and some product-prices are set to zero - i just want to hide the "add to cart" button on these products, so these are not buyable.

already asked this on the support page, but no success

Greets

2
  • Do you have the Code of your single-product.php?
    – fischi
    Commented Aug 8, 2013 at 8:54
  • 1
    Yes, but i dont think it would help since changes on the single-product.php has no effect on this issue. Somebody told me to "create a new function hooked into init which checks a product price, if it's zero remove the add to cart button using remove_action()" So yea, im not good at coding and can't do this on my own. Hope this helps :)
    – user36383
    Commented Aug 8, 2013 at 9:10

2 Answers 2

9

Look at the beginning of the add to cart templates in WooCommerce. At the beginning there is a check to determine whether the product is purchasable. Inside the is_purchasable() method in the product class is a filter. By default the product is not purchasable if there is no price set at all, but that can be extended to cover products for which the price is set to 0.

function wpa_109409_is_purchasable( $purchasable, $product ){
    if( $product->get_price() == 0 )
        $purchasable = false;
    return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wpa_109409_is_purchasable', 10, 2 );
7
  • Thanks alot! This was exactly the solution for my problem!
    – user36383
    Commented Aug 13, 2013 at 7:53
  • @helgatheviking, your many answers over here are priceless. I keep finding again and again! thanks!
    – pcarvalho
    Commented Aug 14, 2016 at 3:37
  • 1
    You're welcome @peteroak, I'm glad you found them useful. I spend too much time here :) (more at SO now in the WooCommerce tag, but still) Commented Aug 14, 2016 at 20:02
  • YAS! I was adding a ridiculous amount of conditionals to template files to check for a proprietary value to see if a user was allowed to purchase a thing. Used this filter instead (which I somehow completely missed) - SOLVED! Thanks, Kathy.
    – indextwo
    Commented Oct 31, 2016 at 13:46
  • Hello @helgatheviking is it possible to add a product description or message on the page for those specific product when hide to cart ?
    – Gregory
    Commented Dec 15, 2020 at 16:36
0

As far as I understood your problem.. woocommerce has an option when you don't put any price, there will be no button at all..
Your product becomes like just a regular gallery/catalog..
Should that's what you're looking for.. perhaps the easiest way would be then to edit your csv file.. just find and replace zeros with empty space..

Not the answer you're looking for? Browse other questions tagged or ask your own question.