Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change cart item prices in Woocommerce 3

I am trying to change product price in cart using the following function:

    add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price' 
     );
      function add_custom_price( $cart_object ) {
         foreach ( $cart_object->cart_contents as $key => $value ) {
         $value['data']->price = 400;
        } 
     }

It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+

How can I make it work in WooCommerce Version 3.0+?

Thanks.

like image 772
Archana Avatar asked Apr 10 '17 13:04

Archana


People also ask

How do I change cart price in WooCommerce?

With WooCommerce version 3.0+ you need: To use woocommerce_before_calculate_totals hook instead. To use WC_Cart get_cart() method instead. To use WC_product set_price() method instead.

How do I get a cart item price in WooCommerce?

Use the WC()->cart method to get woocommerce cart item data. Cart page use this method to load all the values on the cart page e.g. woocommerce get cart item totals, subtotal, get cart items to count, get items price, etc.


2 Answers

Update 2021 (Handling mini cart custom item price)

With WooCommerce version 3.0+ you need:

  • To use woocommerce_before_calculate_totals hook instead.
  • To use WC_Cart get_cart() method instead
  • To use WC_product set_price() method instead

Here is the code:

// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example | optional)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_price( 40 );
    }
}

And for mini cart (update):

// Mini cart: Display custom price 
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {

    if( isset( $cart_item['custom_price'] ) ) {
        $args = array( 'price' => 40 );

        if ( WC()->cart->display_prices_including_tax() ) {
            $product_price = wc_get_price_including_tax( $cart_item['data'], $args );
        } else {
            $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
        }
        return wc_price( $product_price );
    }
    return $price_html;
}

Code goes in functions.php file of your active child theme (or active theme).

This code is tested and works (still works on WooCommerce 5.1.x).

Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.

Related:

  • Set cart item price from a hidden input field custom price in Woocommerce 3
  • Change cart item prices based on custom cart item data in Woocommerce
  • Set a specific product price conditionally on Woocommerce single product page & cart
  • Add a select field that will change price in Woocommerce simple products
like image 168
LoicTheAztec Avatar answered Sep 20 '22 13:09

LoicTheAztec


With WooCommerce version 3.2.6, @LoicTheAztec's answer works for me if I increase the priority to 1000.

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.

I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.

like image 24
Tony Avatar answered Sep 23 '22 13:09

Tony