Hello @avk-1 ,
If I understand correctly, you are trying to figure out how can you target your code for a specific product category page. Is that right?
WooCommerce has a conditional tag is_product_category()
to determine if you are on the product category page. You can learn more about this here.
I hope the information helps.
Thank you 🙂
Thread Starter
Toxic
(@avk-1)
Hi Rashed,
Thank you very much for your response. And sorry for my English.
Unfortunately Conditional tag maybe no solution. I want add m<sup>3</sup>
mark to all price in some category. The code from my prev. post exactly works for standalone products but I need add m<sup>3</sup>
marks to categories.
Could you tell me please what must I’ll change in this code
add_filter( 'woocommerce_get_price_html', 'custom_price_product' );
function custom_price_product($price) {
global $post;
$product_id = $post->ID;
$my_product_array = array(1,2,3,4);
if (in_array($product_id, $my_product_array)) {
$addedtext = '/m<sup>3</sup>';
return $price . '<span class="price-disclaimer">' . $addedtext . '</span>';
}
else {
return $price;
}
}
so that my additional marks appear for categories?
Thread Starter
Toxic
(@avk-1)
Hi Rashed!
I am sorry for disturbing to you by previous message. I partially figured out the problem. Indeed Conditional tags can help to me.
I added next code to functions.php file
add_filter( 'woocommerce_get_price_html', 'custom_price_cat' );
function custom_price_cat($price) {
if ( is_product_category( 'sample-category-slug' ) ) {
$addedtext = 'sample-price-mark';
return $price . '<span class="price-disclaimer">' . $addedtext . '</span>';
}
else {
return $price;
}
}
and on listing page of this product category additional price-marks were displayed. But additional price-marks doesn’t display on single product page. Could you tell me please how do edit my code so that additional price-marks display on listing and the single product page?
Hello @avk-1 m
The conditional tag is_product_category() is applicable for the product archive pages, so it does not work on a single product page.
You should rather take a different approach here and use WordPress’s has_term() function to find out if the product is in a certain category and then add your desired action.
If I put this into your code, it will look like this –
add_filter( 'woocommerce_get_price_html', 'custom_price_product' );
function custom_price_product($price) {
global $post;
$product_id = $post->ID;
if (has_term( 'accessories', 'product_cat', $post->ID)) {
$addedtext = '/m<sup>3</sup>';
return $price . '<span class="price-disclaimer">' . $addedtext . '</span>';
}
else {
return $price;
}
}
I hope the information helps.
Thank you 🙂
Thread Starter
Toxic
(@avk-1)
Thank you very much, sir!
Your code is well and it works excellent!
Thank you!