简体   繁体   中英

Woocommerce: Programmatically Update Item In Cart

I need to programmatically and dynamically change the price of an item in the cart.

I've tried varying combinations of Woocommerce action hooks, the cart and session objects, but nothing quite seems to do the trick. I thought this wouldn't be so challenging.

add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );

function change_cart_item_price( $cart_object ) {

    foreach ( $cart_object->cart_contents as $key => $value ) {

        if( 123 == $value['data']->id ) {

            $new_price = function_to_get_new_price( $value, get_current_user_id( ) );
            $value['data']->price = $new_price;
        }
   }
}

The above code changes the price for each item only on the checkout page, or when updating the cart ( ie: the hook is called when removing an item from the cart ), but not indefinitely.

I'm using the Woocommerce Gravity Forms add-on. I have one product in particular, which will be ordered multiple times by a given user. The user will be allowed 5x free with only shipping fees, and each above 5 will be $20. I have this much coded and functional with Gravity Forms hooks that dynamically populate fields. Shipping is specific to fields within the gravity form, therefore I am leaving that calculation to Gravity Forms.

My issue is that if a user reduces the quantity of this product from their order (removes one of the items from the cart), it should re-calculate the price of each item of the same product within the cart, otherwise they could be over-charged ( an item that used to be the 6th is now the 4th, but the price remains the same, which it shouldn't )

Therefore , I would like to re-calculate the price of each item in the cart, based on the quantity of this particular product, every time something is removed from the cart.

--- EDIT ---

The above code works, but I'm realizing the issue must be a custom loop I'm using to display the prices...

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product = $cart_item['data'];
    if( 123 == $_product->post->ID ) {
        $price_not_updated = $cart_item['data']->price;
    }
}

I figured it out... I looked at the woocommerce cart docs and essentially realized that the prices I was getting had yet to be calculated. So, before running the loop, I had to do the action that I was initially hooking into to change the prices.

Thanks for your help!

function getUpdatedCartPrices() {

    do_action( 'woocommerce_before_calculate_totals', WC()->cart );

    $horray_updated_prices_works = array();

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product = $cart_item['data'];
        if( 123 == $_product->post->ID ) {

            $horray_updated_prices_works[] = $cart_item['data']->price;

        }
    }

}

I changed the function to have a second parameter so that you can use it dynamically with any product as long as you call the proper id. I also set the function return value to either a success or error message. This way you can know if it was changed, and if so, to what price. Hope this helps. by the way, having this function called again when the cart is updated should solve the recalculation issue. It would be great if i could see the code for your $function_to_get_new_price() function.

add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );

function change_cart_item_price($id, $cart_object ) {

    foreach ( $cart_object->cart_contents as $key => $value ) {

        if( $id == $value['data']->id ) {

            $new_price = function_to_get_new_price( $value, get_current_user_id( ) );
            $value['data']->price = $new_price;

            $successMsg = "The price of item $value['data']->id was set to $value['data']->price".;
           return $successMsg;

        }else{
           $errorMsg = "Price was not changed!";
           return $errorMsg;
        }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM