How to display woocommerce sale price or regular price if there is no sale price

前端 未结 3 1574
有刺的猬
有刺的猬 2020-12-29 09:34

I\'m using a woocommerce product addons plugin where I would like to display the price of a product in my dropdown section of the addons. Currently the code I have is this

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 10:20

    This article worked for me. you can also try this one.

    If you want to get the regular price or sale price of a woocommerce product and you are getting nothing you need to know the following:

    If the product has no variations you can get the regular price and sales price just like that:

    Get the price of a simple procut

    regular_price;
    echo $product->sale_price;
    ?>
    

    If the product has variations you will get nothing if you will use the code above.

    You need to get the variation product prices.

    Get the price of a product with variations

    #1 Get product variations
    $product_variations = $product->get_available_variations();
    
    #2 Get one variation id of a product
    $variation_product_id = $product_variations [0]['variation_id'];
    
    #3 Create the product object
    $variation_product = new WC_Product_Variation( $variation_product_id );
    
    #4 Use the variation product object to get the variation prices
    echo $variation_product ->regular_price;
    echo $variation_product ->sale_price;
    

    That should be all.

    Enjoy.

    Source: http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/

提交回复
热议问题