In this simple tutorial I shall show you how you will hide the product price and add to cart button without editing the WooCommerce plugin.
Following steps will work with any themes.
Creates New PHP file
At first we shall create a new file “no-add-to-cart-button.php” and save into yourthemefolder/woocommerce folder. If woocommerce folder is not available in your theme folder, you will create it. Here is the code of that PHP file:
1 2 |
<?php // Silence is golden. |
Scenario 1: Hiding Price & Button For All Customers.
Open your functions.php file and drop the following code at end of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/** * Hides the price and add-to-cart button from the product * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ add_action( 'init', 'paulc_hide_price_add_to_cart_button', 1000 ); function paulc_hide_price_add_to_cart_button() { add_filter( 'woocommerce_get_price_html', '__return_false' ); add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } /** * Hides the add-to-cart button from the single product page * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; } |
Scenario 2: Hiding Price & Button Only For Logged Out Customers
Open your functions.php file and drop the following code at end of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/** * Hides the price and add-to-cart button from the product * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ add_action( 'init', 'paulc_hide_price_add_to_cart_button', 1000 ); function paulc_hide_price_add_to_cart_button() { if( is_user_logged_in() ) return; add_filter( 'woocommerce_get_price_html', '__return_false' ); add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } /** * Hides the add-to-cart button from the single product page * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; } |
Scenario 3: Hiding Only Add To Cart Button
Open your functions.php file and drop the following code at end of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * Hides the add-to-cart button from the product * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ add_action( 'init', 'paulc_hide_add_to_cart_button', 1000 ); function paulc_hide_add_to_cart_button() { add_filter( 'woocommerce_loop_add_to_cart_link', '__return_null' ); add_filter( 'wc_get_template', 'paulc_wc_get_template', 90, 2 ); } /** * Hides the add-to-cart button from the single product page * * @copyright paulchinmoy.com * @author Paul Chinmoy **/ function paulc_wc_get_template( $located, $template_name ) { $new_path = get_stylesheet_directory_uri() . '/woocommerce/no-add-to-cart-button.php'; $templates = array( 'single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/external.php' ); if( in_array( $template_name, $templates ) ) { return $new_path; } return $located; } |
muhammed suphi avfizade says
hello
thanks a lot
how to hide price and display login/register button
for new user or logged out ones
Louis-Philippe says
Namaste (*, really appreciated.
Louis-Philippe says
Thank you, I needed that!
Now i wish to hide the ADD TO CART if product is virtual.
I can’t get it. Can you help ?
Paul says
Try this code
Lp says
Thank you. Clever but only working on archive product page. I already really appreciate your help.
Paul says
For single product details page you will target the wc_get_template filter and write the custom code like above script
Louis-Philippe says
Thank you again, i’m still mistify cause the $product can’t found null … I surely owe you at least a beer 😉
Paul says
Here is the code for single product page
These days there have no value for work.