I try to show regular price before add to cart. This code must work only for one catgory. How to change this code?
add_action('woocommerce_before_add_to_cart_form', 'basic_price', 15 );
function basic_price() {
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$formatted_price = wc_price( $price );
$product_categories = array('laminat');
if( has_term( $product_categories, 'product_cat', $_product->get_id() ) )
echo '<span class="ri ri-clock">Стоимость упаковки: <span class="woocommerce-Price-amount amount">'. $formatted_price .'</span></span>'; // Print new html with title and price
}
this code works but for all categories:
add_action('woocommerce_before_add_to_cart_form', 'basic_price', 15 );
function basic_price() {
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$formatted_price = wc_price( $price );
echo '<span class="ri ri-clock">Стоимость упаковки: <span class="woocommerce-Price-amount amount">'. $formatted_price .'</span></span>'; // Print new html with title and price
}
My Answer:
To make the code work only for a specific category, you can modify the if condition to check if the product belongs to that category before displaying the regular price. Here's the updated code:
php
add_action('woocommerce_before_add_to_cart_form', 'basic_price', 15 );
function basic_price() {
global $product;
$price = $product->get_regular_price();
$formatted_price = wc_price( $price );
$product_categories = array('laminat');
if( has_term( $product_categories, 'product_cat', $product->get_id() ) ) {
echo 'Стоимость упаковки: '. $formatted_price .''; // Print new html with title and price
}
}
In this updated code, we are using `$product->get_regular_price()` to get the regular price of the product. We are also checking if the product belongs to the 'laminat' category before displaying the regular price.
Rate this post
4 of 5 based on 9630 votesComments