简体   繁体   中英

Displaying custom text with price in short description product WooCommerce

I am trying to create an automatic text in the sort description of the WooCommerce products and put the price of the product in this message.

For example: "buying this product you get 50 points."

In this example, he earns 50 points because he is in a product that costs $ 50

Here is the code I am using:

add_action( 'woocommerce_single_product_summary', function() {
   echo 'Buying this product you get X points';
}, 25 );

I put this code in the functions.php, but it only shows the text, I could not put the price of the product too.

Could someone tell me how to do this?

Woocommerce has a function just for that. You just need to declare it. The function is get_price_html() will return the price with the currency format, if you want it without those you can just use the $product->get_price(); and just have to declare global $product; before using it. Here is the code i used.

function PricePoints(){
  global $product;
  echo 'Buying this product you get '.$product->get_price().' points';
}
add_action( 'woocommerce_single_product_summary','PricePoints',25);

This is how it gets displayed in my theme (in your theme will be different) 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM