Woocomerce hide price for specific Category - php

I would like to hide prices for products in specific categories, prices should be hidden only on shop and category page, but visible on product page, chart and subtotals.
Thank you!
This code has this functionality but I would like to apply it only on specific categories:
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 );
}

You can check if the product has a category by has_term function, and it's an action hook not a filter
add_action( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
$category_name = 'the_category_name_here';
if ( has_term( $category_name, 'product_cat' ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}

You can get the cat name if you have the prodict id with the following:
$term = get_term_by( 'id', $cat_id, 'product_cat' );
echo $term->name;
So simply write a condition to the cat name
function remove_woocommerce_loop_price() {
if($term->name == 'your_category')
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}

To hide the price based on specific product category slugs, you can use this:
// hide the price based on product categories
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
function hide_price_for_specific_product_categories() {
// set the slugs of the product categories for which prices must be hidden
$categories = array(
'clothing',
'bags',
'shoes'
);
global $product;
// if the product belongs to at least one product category present in the "$categories" array, it hides the price
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
To hide the price based on specific product category ids, you can use this:
// hide the price based on product categories
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
function hide_price_for_specific_product_categories() {
// set the ids of the product categories for which prices must be hidden
$categories = array( 61 );
global $product;
// if the product belongs to at least one product category present in the "$categories" array, it hides the price
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
The code has been tested and works. Add it to your theme's functions.php.

Related

Show custom field between product name and star ratings on WooCommerce archive pages

I want to add a custom field on product cards like below. The code I wrote for this works, but the custom field I added appears above the category name.
I want it to appear between the stars and the product name as I showed in the picture.
The code I am currently using for this is
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_show_product_id', 20);
function woo_show_product_id() {
global $product;
echo $product->id;
}
How can I do that or what adjustment is needed for this?
In includes/wc-template-hooks.php we can find:
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
So change priority from 20
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_show_product_id', 20);
To 4 to display your custom field just before the star ratings
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_show_product_id', 4 );
Priority: Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10
Note: use $product->get_id(); as of WooCommerce 3.0
So to answer your question, you can use
via woocommerce_after_shop_loop_item_title
function woo_show_product_id() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get Product ID
echo $product->get_id();
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_show_product_id', 4 );
OR
some other hook from includes/wc-template-hooks.php
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
And then you get with priority 11
via woocommerce_shop_loop_item_title
function woo_show_product_id() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get Product ID
echo $product->get_id();
}
}
add_action( 'woocommerce_shop_loop_item_title', 'woo_show_product_id', 11 );

WooCommerce Hide Price for only certain categories [duplicate]

In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if(is_product_category('sold')){
$price = '';
return $price;
}
}
To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
$price = '';
return $price;
}
It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.
Code goes in function.php file of your active child theme (or active theme).
Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:
// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
global $product;
if( is_product_category('sold') ):
// Hide prices
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Hide add-to-cart button
remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );
endif;
}
// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
global $product;
if( has_term( 'sold', 'product_cat', $product->get_id() ) ):
// Hide prices
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
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_add_to_cart_button', 20 );
}
endif;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.

Disable links for a specific product category in Woocommerce shop

How can I disable link to woocommerce single product page for a specific category?
I mean want to disable the option of clicking to open single product page for a specific category not all categories. So, there's some categories I need it to work with single page as normal.
To clarify: I want in this category to show product photo and name and disable when try to click to open product page.
For a defined product category (or product categories) the following code will:
Remove the link on the product
Remove the button under the product photo
in shop and archive pages.
You will have to define in both functions your targeted product category (or product categories):
add_action( 'init', 'custom_shop_loop_items' );
function custom_shop_loop_items() {
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'custom_template_loop_product_link_open', 10 );
}
// Removing the product link
function custom_template_loop_product_link_open() {
global $product;
// HERE define your product categories in the array (can be either IDs, slugs or names)
$terms = array( 't-shirts', 'hoods' ); // Coma separated
$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
if( has_term( $terms, 'product_cat', $product->get_id() ) )
echo '<a class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
else
echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
// Removing the product button
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
// HERE define your product categories in the array (can be either IDs, slugs or names)
$terms = array( 't-shirts', 'hoods' ); // Coma separated
if( has_term( $terms, 'product_cat', $product->get_id() ) )
$button = '';
return $button;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Hide Price based on product category in Woocommerce

In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if(is_product_category('sold')){
$price = '';
return $price;
}
}
To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
$price = '';
return $price;
}
It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.
Code goes in function.php file of your active child theme (or active theme).
Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:
// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
global $product;
if( is_product_category('sold') ):
// Hide prices
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Hide add-to-cart button
remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );
endif;
}
// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
global $product;
if( has_term( 'sold', 'product_cat', $product->get_id() ) ):
// Hide prices
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
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_add_to_cart_button', 20 );
}
endif;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.

WooCommerce product price visibility: add price back only for a specific category

The website I am working on requires you to be logged in to see prices and I've been using a plugin to do this. However, I was just thrown a curve ball and told that one specific category on the website must have prices shown all the time, regardless if the user is logged in or not.
It looks like the plugin uses
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
and
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
to remove the prices. And this is how I am trying to re-add the prices in for products in the specific category:
function make_surplus_price_always_visible(){
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'surplus-allison-parts', $categories ) && !is_user_logged_in()) {
?>
<script>
alert('product in surplus');
</script>
<?php
//add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}
}
add_action('woocommerce_after_shop_loop_item_title', 'make_surplus_price_always_visible', 50);
but it doesn't add the prices back. The jQuery alert is working so it's not a matter of meeting the "if" statement requirements.
How can I go about adding back the product prices for a specific category?
Updated: Here is the correct way to make it work:
Is better and shorter to use has_term() Wordpress function for the product category.
Removed your javascript message (that was there for testing purpose).
The hook priority needs to be just before 10 (the price hook priority) to work.
The code:
add_action('woocommerce_after_shop_loop_item_title', 'shop_loop_make_surplus_price_always_visible', 8 );
function shop_loop_make_surplus_price_always_visible(){
global $post;
// Set here your product categories (Names, slugs or IDs) in this array
$categories = array( 'surplus-allison-parts' );
if ( has_term( $categories, 'product_cat', $post->ID ) && ! is_user_logged_in()) {
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}
}
And:
add_action('woocommerce_single_product_summary', 'single_product_make_surplus_price_always_visible', 8 );
function single_product_make_surplus_price_always_visible(){
global $post;
// Set here your product categories (Names, slugs or IDs) in this array
$categories = array( 'surplus-allison-parts' );
if ( has_term( $categories, 'product_cat', $post->ID ) && ! is_user_logged_in()) {
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works

Categories