(Woocommerce) Post custom price to cart
-
In addition to using product variations, my client was in need of shipping variations that differ between products. I created a couple functions to take the product variation price (subtotal), and based off the shipping variation selected, I add the shipping price to the subtotal therefore creating a “total” price.
I then found this bit of code to post the new total value to the cart amount:
add_action( ‘woocommerce_before_calculate_totals’, ‘add_custom_total_price’ );
function add_custom_total_price( $cart_object ) {
global $woocommerce;
$custom_price = $_POST[‘totalAmount’]; // This is my custom priceforeach ( $cart_object->cart_contents as $key => $value ) {
$value[‘data’]->price = $custom_price;
}
}This works well at first, but when I refresh the cart page, the total from the cart goes back to $0.00, so it seems that my price is not being stored properly on “add to cart” post.
I have been searching through the WC_Cart class functions and saw that persistent_cart_update() might be the function to solve my problems. In the function above after $value[‘data’] is assigned I put the code outside of the foreach loop:
$woocommerce->cart->persistent_cart_update();This has no affect on the refresh loss. How else do I go about making sure my new custom price gets stored in session and can be passed between cart > checkout?
(let me know if more code is needed, I would be happy to share, been working on this for 5 days now.)
An example of this can be found at:
https://www.gojpg.com/product/giant-flag-x-large/
- The topic ‘(Woocommerce) Post custom price to cart’ is closed to new replies.