Get the product price in Woocommerce 3

别来无恙 提交于 2019-12-01 06:34:58

问题


I'm trying to get price without currency in a function I made.

function add_price_widget()
{
    global $woocommerce;
    $product = new WC_Product(get_the_ID());
    $thePrice = $product->get_price_html();

    echo thePrice;
}

Displays: 100kr

How do I get it to just give me the price 100


回答1:


What @Syntax_Error had said is correct you have to use get_price(), WooCOmmerce also provide a wrapper function wc_get_product() for WC_Product class.

So your function would look something like this:

function add_price_widget()
{
    $product = wc_get_product(get_the_ID());
    $thePrice = $product->get_price(); //will give raw price
    echo $thePrice;
}

Hope this helps!




回答2:


You can just use the function get_price that returns only the number (without dots or symbol)

function add_price_widget() {
 global $woocommerce;
 $product = new WC_Product(get_the_ID()); 
 $thePrice = $product->get_price();
 echo thePrice;
}

I just tested it in my site and it work. So it should work for you too.



来源:https://stackoverflow.com/questions/43529111/get-the-product-price-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!