Hiding prices on the Woocommerce shop pages - php

I'm using WordPress with a WooCommerce plugin and I would like to hide the prices from the shop page (Eg. $20 - $50). I have tried researching it but I haven't found much relating to this problem.
I only want to hide the prices on the shop page, not the individual product pages.
Any help provided will be much appreciated.

You can use this simple hooked function that will remove all product prices from Woocommerce archive pages as shop, product category archives and product tag archives pages:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
If you want to only target Shop pages, you will have to make it this way:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
if( ! is_shop() ) return; // only on shop pages
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
Updated: You may also want to replace add to cart button by a linked button to the product in Shop and archives pages
// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Not needed for variable products
if( $product->is_type( 'variable' ) ) return $button;
// Button text here
$button_text = __( "View product", "woocommerce" );
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

Related

Change "add to cart" text if a user has bought specific products in WooCommerce

I wanted to know if there is a way to replace the woo-commerce "add to cart" button with custom text and url for a specific product if the user has bought that specific product.
and I want it happens everywhere (on shop page,product page , ...)
Try the following that will change the product "add to cart" text if that product has already been bought by the customer on single product pages and archive pages
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Archive product pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Single product pages
function custom_add_to_cart_text( $button_text, $product ) {
$current_user = wp_get_current_user();
if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
$button_text = __("Custom text", "woocommerce");
}
return $button_text;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
Now you can also restrict that to specific product ids like:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Archive product pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Single product pages
function custom_loop_add_to_cart_button( $button_text, $product ) {
$current_user = wp_get_current_user();
// HERE define your specific product IDs in this array
$specific_ids = array(37, 40, 53);
if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() )
&& in_array( $product->get_id(), $specific_ids ) ) {
$button_text = __("Custom text", "woocommerce");
}
return $button_text;
}
Code goes in functions.php file of your active child theme (or active theme). it should work.
Some related answers threads:
Change "add to cart" button for purchased products in Woocommerce
Custom add to cart button, if the customer has bought previously the product
Checking if customer has already bought something in WooCommerce

Customize Add to Cart button on specific WooCommerce product categories

I am trying to change the add to cart button for a specific product category on my WooCommerce archives pages to a "Read more" button that will be linked to the product page (but Not on the single product page itself).
(Woocommerce, with Elementor on Wordpress)
Using "Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3" answer code, how to restrict it to only one product category (the term name is "Classes" in this case)?
Any help is appreciated.
You can use has_term() conditional function to target a product category, this way:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( has_term( 'Classes', 'product_cat', $product->get_id() ) ) {
$button_text = __("Read more", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme) or also in any plugin file.

Remove Add to Cart button keeping stock info in Woocommerce single product pages

In WooCommerce I am trying to remove add to cart from product keeping the stock information.
I tried to do it with a plugin but it also remove the stock information.
Is there any way to remove Add to Cart button keeping stock information in single product pages ?
The following code will remove add to cart form from single product on simple product type but will display the stock information:
// Single products (Simple): remove add to cart button and keep stock info
add_action( 'woocommerce_single_product_summary', 'remove_simple_product_add_to_cart_button', 1 );
function remove_simple_product_add_to_cart_button() {
global $product;
// For simple products type
if( $product->is_type( 'simple' ) && $product->is_purchasable() ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'show_stock_info', 30 );
}
}
// Function that stock info
function show_stock_info() {
global $product;
echo wc_get_stock_html( $product );
}
Code goes in function.php file of your active child theme (active theme). tested and works.

Changing Woocommerce "Add to cart" button to "view product" button

Trying to change the woocommerce button text from "add to cart" to "read more" and redirect it so that clicking the button takes the user to the individual product page. So far, the link works but all the text on the button says is "Button" when I need it to say "Read More". I'll place the code below, can anyone please tell me what the problem is.
/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('<br>[button link="' . esc_attr($link) . '"]Read More[/button]');
}
Try this alternative that will replace add to cart button by a linked button to the product in Shop and archives pages
// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Not needed for variable products
if( $product->is_type( 'variable' ) ) return $button;
// Button text here
$button_text = __( "View product", "woocommerce" );
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
Use this code to replace the default “Add to Cart” button with “Read More” (or anything you like) linking to the single product page.
// First, remove Add to Cart Button
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
// Second, add View Product Button
add_action( 'woocommerce_after_shop_loop_item', 'shop_view_product_button', 10);
function shop_view_product_button() {
global $product;
$link = $product->get_permalink();
echo 'View Product';
}
You can add this PHP Snippet at the very bottom of your active child theme (or main theme) functions.php file. Code Source

Hide product prices and add-to-cart buttons but not variations for unregistered users in WooCommerce

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.

Categories