How to update/override woocommerce cart price with code
Use the woocommerce_cart_calculate_fees
hook to update the woocommerce cart price.
add_action( 'woocommerce_cart_calculate_fees', 'cp_add_custom_price' );
In the function get product details and update with your new price like below.
function cp_add_custom_price( $cart_object ) { global $woocommerce; foreach ( $cart_object->cart_contents as $key => $value ) { //Get the product data like below $proid = $value['product_id']; $variantid = $value['variation_id']; $price = $value['data']->price; //check your condition if($youcondition == true ) { //update with the new price $value['data']->price = $newprice; } } }
To add an extra fees to woocommerce cart use the add_fee()
function in the same hook like below.
$excost = 5.99; $woocommerce->cart->add_fee( 'Express delivery', $excost, true, 'standard' );
This is working example to add a special fee for products from category with id 10
function cp_add_custom_price( $cart_object ) { global $woocommerce; $specialfeecat = 10; // category id for the special fee $spfee = 0.00; // initialize special fee $spfeeperprod = 5.00; //special fee per product foreach ( $cart_object->cart_contents as $key => $value ) { $proid = $value['product_id']; //get the product id from cart $quantiy = $value['quantity']; //get quantity from cart $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts if ( $terms && ! is_wp_error( $terms ) ) : foreach ( $terms as $term ) { $catid = $term->term_id; if($specialfeecat == $catid ) { $spfee = $spfee + $quantiy * $spfeeperprod; } } endif; } if($spfee > 0 ) { $woocommerce->cart->add_fee( 'Special fee', $spfee, true, 'standard' ); } } add_action( 'woocommerce_cart_calculate_fees', 'cp_add_custom_price' );
I am trying to add specific fees depending on the cart subtotal. I have figured out how to add a flat rate of $7.99 for orders <=8.99.
I would like to add a 90% fee to orders $9.00-$24.99, 80% fee for order ranging from $25-$49.99, 65% fee for orders ranging from $50-$99.99.
Can you please help me figure this out? I've been researching and experimenting all day to no avail.
Thanks so much for all your informative posts!
Please try the this code to get cart subtotal then check your condition
$cart_subtotal = $woocommerce->cart->get_cart_subtotal();
or print the entire cart object array and get the sub total
Hope that helps !
hey hi,
thanks for a good snippet š
but i am facing a problem in implementing it in my situation.
i have to include a certain insurance amount to total, that will be user selectable through a checkbox on checkout page, i.e. if a user wants insurance he will select the checkbox and total will be updated.
please let me know how can i do this š
Im trying to add a fixed “Next Day Delivery” charge on top of Postcode Shipping prices I have set up. Will this code work for me? I need it to only added if say a checkbox is ticked.
Where exactly does this code go?
Sorry for the newbie question, but where do I add the code?
Sorry for the late reply, the code will go in theme’s/child theme’s functions.php
Thanks
Can you point me in the right direction for adding a fixed fee where the cart total is greater than 0.00 please?
I have some items that come through as free, so don’t want a fee attaching to these but I want it to add one to everything else.
Thanks
I’ve worked it out now, thanks.
Glad you you figured it out, sorry for the late reply
How can I use the cp_add_custom_price only after checking one custom field I added in order using the woocommerce_after_order_notes but before the woocommerce_checkout_process so the user can see the new fee ?
Have you tried to trigger the function using ajax ?
I tried to by adding script using woocommerce_after_order_notes but don’t know what to call, it is the first time in Woocommerce.
By using hooks like woocommerce_review_order_after_shipping and woocommerce_cart_calculate_fees I can add the fee I want but without checking the fields. I tried many hooks but can’t find out how to check if the custom field is filled or empty in checkout.
Being honest this need to workout, not sure about the possibiltiy of modifying cart values from checkout page. You may try to add a filed to cart page itself. This may give you some hints (not tested)
https://gist.github.com/anonymous/81ee68a8affcc7ccff0a
Thanks
Thanks for the reply.
It could be in the cart page, didn’t think of it. I will try it
Hi, I was looking for some code like this. I have a question: How can I make the extra fee optional for customers with an extra “check” field? If the fee is shipping ensurance, I want that customers able to choose if they want to pay the ensurance or not. The amount of the fee must to be a percentage of products total price (without shipping cost).
Thanks in advance.
Thanks for your code! I want to make conditions to when a fee appears. Do you know how that works? I have the following code, but it’s not working:
function verzendkosten( $cart_object ) {
global $woocommerce;
$laag = 37.50;
$hoog = 57.50;
$vala = 550;
$prijs = $woocommerce->cart->total;
if($prijs cart->add_fee( 'Verzendkosten', $hoog, true, 'standard' );
}
if($prijs > $vala) {
$woocommerce->cart->add_fee( 'Verzendkosten', $laag, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'verzendkosten' );