Hey guys,
These few months have been quite busy and was not able to share snippets or codes along the way, nor was able to write for the personal blog. But for the last week I have been working on a WooCommerce theme that has been totally customized.
People usually want to get the pricing of products and want to wrap it in their own html and styles. So the basic code (below) does not work.
<?php echo $product->get_price_html(); ?>
So I am sharing with you guys a code that can be used for WooCommerce product pricing display. This code has been tested upto Version 2.0.14. I’ll explain the code below.
<?php //get the sale price of the product whether it be simple, grouped or variable echo $sale_price = get_post_meta( get_the_ID(), '_price', true); //get the regular price of the product, but of a simple product $regular_price = get_post_meta( get_the_ID(), '_regular_price', true); //oh, the product is variable to $sale_price is empty? Lets get a variation price if ($regular_price == ""){ #Step 1: Get product varations $available_variations = $product->get_available_variations(); #Step 2: Get product variation id $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation. #Step 3: Create the variable product object $variable_product1= new WC_Product_Variation( $variation_id ); #Step 4: You have the data. Have fun$regular_price = $variable_product1 ->regular_price; } echo $regular_price;
So, what you see above is we have printed/echoed he sale and the regular price and if you can read between the lines the code is pretty self-explanatory.
Good Luck
Hi great article
What file do I insert the above code?
Regards,
Andrew
Simple COPY/PASTE
Hello Ali,
Thank you very much for this. Very handy. It works great and with the information I get from your code I’m going to create a function which calculates the discount you get when upgrading the number of kilograms.
Regards,
Remi
I am glad, I could be of some help
Thanks! This put me on the right track. However, it was displaying both the sale price and regular price for me. So I made a few edits:
get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
where do i paste this code please?
Again, you can place this where you want to display the details, could be product single or any other file
This is exactly what I’m looking for. Thanks a lot.
Alihs, thanks for the great input! Would you be so kind to explain to a dummy, where should I copy/paste your code exactly. ? I spent hours figuring out with no success:(
Thanks in advance
You can place it wherever your product is, couple be the single product or any other file.
paste it in functions.php file. It works for me
Hello Ali,
I was trying to use your code above to get the price of the selected variation, but it only gives me the price of the 1st variation. When I select a different option from my drop down color list the price does not get updated.
My code is attached to the following filter
add_filter(“gform_pre_render_5”, “set_column”, 20, 5);
can you please tell me how I can modify the code to display the current active price. I need to use this value in my calculations
Thanks a lot
Can someone please help me with the following?
How do I add the variation names to the variable prices.
Currently showing as
$20.00–$30.00
and should look like this:
MP4: $20.00 – DVD: $30.00
The attribute name is Video, with the values of DVD and MP4