Skip to content

Instantly share code, notes, and snippets.

@michaeltieso
Created September 22, 2014 20:17
Show Gist options
  • Save michaeltieso/0068bf05f4ad64577e54 to your computer and use it in GitHub Desktop.
Save michaeltieso/0068bf05f4ad64577e54 to your computer and use it in GitHub Desktop.
Dynamic Pricing + Product Add-ons
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$addons = $value['addons'];
$add_to_price = 0;
foreach($addons as $addon) {
$add_to_price .= $addon['price'];
}
$price = $value['data']->price;
$price += $add_to_price;
$value['data']->price = $price;
}
}
@brotsky
Copy link

brotsky commented Sep 24, 2014

Thanks Michael for the credit!

@lucindabrown
Copy link

Michael and Brandon,
Thanks for finding the solution and posting this. You saved me a lot of hunting around and helped me save the day.
You guys both just earned some good karma. Thank you!

@nickkuijpers
Copy link

Any idea about a discount possibility? Options that lowers the price instead of +?

@JediV11
Copy link

JediV11 commented Jul 15, 2015

Hey guys! Thanks for the code. I'm having an issue when I try to add more than one product addon? Seems to work with one but not two charges. Any solutions?

@Rbbiz
Copy link

Rbbiz commented Mar 14, 2016

I've added the code via code snippets plugin, with no effect whatsoever. Any suggestions?

@kemmieg
Copy link

kemmieg commented Apr 16, 2016

How can I make this work with Gravity Forms Product Add-Ons? It works great when you use the Woocoommerce add-ons field but not when you are using Gravity Forms for the add-ons. Any suggestions?

@lucasstark
Copy link

@kemmieg
I just came across this thread. Gravity Forms Product Addons does this automatically if you select the "Enable Dynamic Price" when attaching a form to a product. It was an option I added in early 2016.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment