V
V
Valentina2020-12-18 19:42:57
PHP
Valentina, 2020-12-18 19:42:57

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
}


Write here such an option for one category, but everything flies. What is wrong with my 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
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Litvinenko, 2020-12-18
@valentinadikaya

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 question

Ask a Question

731 491 924 answers to any question