• 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 price

    foreach ( $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/

Viewing 4 replies - 1 through 4 (of 4 total)
  • hi,

    Thank you for sharing the article.

    I am facing the same issue. I tried your solution but that didsn’t help me out. Can you please guide me what can be wrong? Or if you can share your code it will be great.

    Code which I have used is as below:
    add_action( ‘woocommerce_before_calculate_totals’, ‘add_custom_price’, 10, 2 );

    function add_custom_price( $cart_object ) {
    global $woocommerce;
    $product_id = $_POST[‘product_id’];
    foreach ( $cart_object->cart_contents as $key => $value ) {
    if ( $value[‘product_id’] == $product_id ) {
    $value[‘data’]->price = 500 ;
    }
    }
    $woocommerce->cart->persistent_cart_update();
    }

    Appreciate a quick response.

    Thanks & Regards,
    Kruti

    Thread Starter iamjds

    (@iamjds)

    What are you trying to do exactly? I don’t mind sharing my code, but the purpose of me posting this article was because I did not find a solution either.

    When I contacted WooThemes to assist me with this issue, they could not offer me assistance because I have not purchased any of their products.

    The main problem I am having is it initially posts the new total price to the cart widget, but when I go to the cart.php page, the value is $0.00.

    I’m sorry I cannot be of any other assistance at this time. Definitely interested in hearing a solution if you find one.

    Hi,

    Thank you for reply.

    I am trying to use $_POST value in hook. But in cart page it display as 0.00

    If I use static price it works fine but not with $_POST ??

    I am working on this issue since 3 days. Will surely share solution if I find any but still I am struggling.

    Regards,
    Kruti

    Did you find any solution for the problem. I am having the same problem right now. So can you please help me through it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘(Woocommerce) Post custom price to cart’ is closed to new replies.