WooCommerce display price before add to cart

I’m trying to make the WooCommerce product price display before the “add to cart” button, however, I cant seem to get the price to display.

Here is the code I’m using in my functions.php

add_action( 'woocommerce_before_add_to_cart_button', 'misha_before_add_to_cart_btn' );
 
function misha_before_add_to_cart_btn(){
    echo '<div class="btn-price">'. $product->get_price_html().'</div>';
}

Please would someone be able to point out where I’m going wrong with the code that I’m using above.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The variable $product is undefined when your function runs, you need to access the object to call the method get_price_html(). One way to do it is to call the global variable:

add_action( 'woocommerce_before_add_to_cart_button', 'misha_before_add_to_cart_btn' );
function misha_before_add_to_cart_btn(){
  global $product;
  echo '<div class="btn-price">'.$product->get_price_html().'</div>';
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x