In content-product.php, products from the current category page have a "add to cart"-button if the item has a price.
I want to remove this button, BUT only on the items that returns true on the if statement around it.
I tried:
$showBuyButtonNo = get_field('show_buy_button');
if ($showBuyButtonNo) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
BUT this does it to all items.
You can update default WooCommerce content-product.php as below:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
// replace a_category and another_category with the slugs of the categories you'd like to have the button removed from
if( is_product_category( array( 'a_category', 'another_category'))) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
Reference: https://www.wpblog.com/add-to-cart-button-in-woocommerce-store/
Reference: https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305
Related
Im trying to hide the add to cart button in woocommerce for all products except a varibale product i have. I have tried the following which leaves the variable select options (which is what i want) but it hides the add to cart button (which i don't want).
add_filter( 'woocommerce_is_purchasable', '__return_false' );
add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_variable_product', 1, 0 );
function hide_add_to_cart_button_variable_product() {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
}
Is there a way to do this?
All my products are simple products except for this single variable products, so maybe there is a function to hide the cart button for all simples except variables?
add_action('woocommerce_single_product_summary', 'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if ( !empty($product) && $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
I think you can just edit the simple.php template file in the add-to-cart directory so it wouldn't show the button for simple products.
This is how it all looks
I want to add a new "add_to_cart" button template for the outer product card without affecting the inner one. but when I add, the old one remains. What's wrong with my code?
it is my code in functions.php
function loop_custom_variable_products() {
global $product;
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 30 ); // Remove price
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30); // remove add to cart
remove_action( 'woocommerce_after_shop_loop_item', 'loop_variations_custom_buttons_and_prices', 30 ); // Add prices with custom buttons
}
add_action( 'init', 'loop_custom_variable_products' );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_variable_add_to_cart1', 30 );
//add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_variable_add_to_cart1', 30);
function woocommerce_variable_add_to_cart1(){
//... some code for new add to card style
}
On my site, I want to hide "Add to Cart" button only for logged out user /"Guest Users"
This is the code that I have added to my functions.php file into my theme.
// For product archives pages
add_action( 'init', 'hide_product_archives_prices' );
function hide_product_archives_prices(){
if( is_user_logged_in() ) return;
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10) ;
}
//
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
if( is_user_logged_in() ) return;
global $product;
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
if( ! $product->is_type('variable') ){
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
} else {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10);
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
Everything works fine as it hides the add to cart button for logged out users, but i still want the quantity selector to be available, because I am using a Request a quote plugin to add the product with the quantity to a quote cart. All products are variable products
At this stage the enquire now button shows, but when you click it to add it to the quote cart it doesn't add it because there's no quantity selected.
In my WooCommerce shop I want to hide the prices until the customer has logged in. I've got the following code working that does exactly that:
add_action('init','hide_price');
function hide_price(){
if(!is_user_logged_in()){
remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart',10);
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);
remove_action('woocommerce_single_product_summary','woocommerce_template_single_price',10);
remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
add_action('woocommerce_single_product_summary','print_login_to_see',31);
add_action('woocommerce_after_shop_loop_item','print_login_to_see',11);
}
}
function print_login_to_see(){
echo '' . __('Login to see prices','theme_name') . '';
}
However this also removes the variation drop down and I would like to keep that.
Is there any direct way to keep the variation drop down menu but still hide the prices until the customer has logged in?
Thanks
Updated:
You will need to separate shop and archives pages from single product pages. In single product pages you will target variable products to add and remove the specific hooked functions.
Last thing, in your function print_login_to_see(), woocommerce_get_page_id() is obsolete and it's replaced by wc_get_page_id() …
So your code will look to this:
// For product archives pages
add_action( 'init', 'hide_product_archives_prices' );
function hide_product_archives_prices(){
if( is_user_logged_in() ) return;
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10) ;
add_action ( 'woocommerce_after_shop_loop_item', 'print_login_to_see', 10 );
}
//
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
if( is_user_logged_in() ) return;
global $product;
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
if( ! $product->is_type('variable') ){
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary','print_login_to_see', 30 );
} else {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10);
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'print_login_to_see', 20 );
}
}
// Display a my account link
function print_login_to_see(){
echo '' . __('Login to see prices','theme_name') . '';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works.
I'm working on a woocommerce theme and I'm required to hide the add to cart button for products that have 0 as price, as these products may only be inquired and not added to cart. I have successfully hidden the 'add to cart' button on the product page however, I am having a hard time doing so on the shop page/category page.
Below is my code for filtering the add to cart as well as changing the default 'Free!' message.
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
/**
* Changes woocommerce default 'Free!' to return message
*/
function hide_free_price_notice( $price ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
return 'Please inquire for pricing';
}
I have also tried filtering all add to cart buttons on loop, however this did not work either.
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
Any suggestions? I'm looking to see if I can hide it with CSS...
This is what I use to change the 'Free!' message to 'POA' and also hide the cart.
Note: There seems to be an issue with this on WooCommerce version V2.1?
* Swop the 'Free!' price notice and hide the cart with 'POA' in WooCommerce
*/
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
function hide_free_price_notice( $price ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
return 'POA';
}