Answer the question
In order to leave comments, you need to log in
How to put base price in arbitrary place of woocommerce product page?
I need to place the base price of a product above the buy button, but only for products in a certain category.
I have a code but it works 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
}
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
}
Answer the question
In order to leave comments, you need to log in
Everything is correct, the check for a term does not want to work with a variable, this is how it works:
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 );
if( has_term( ['laminat'], 'product_cat' ) ) {
echo '<span class="ri ri-clock">Стоимость упаковки: <span class="woocommerce-Price-amount amount">'. $formatted_price .'</span></span>'; // Print new html with title and price
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question