最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - WooCommerce display price before add to cart

programmeradmin6浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

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.

Share Improve this question edited Sep 25, 2020 at 11:38 Jordan Kellet asked Sep 25, 2020 at 11:29 Jordan KelletJordan Kellet 31 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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>';
}
发布评论

评论列表(0)

  1. 暂无评论