WooCommerce 3.0 can no longer modify cart item price inside woocommerce_before_calculate_totals

As of WooCommerce 3.0 it appears that Cart Contents price is now a protected variable. Attempting to overwrite it does not work anymore.

Code used below…

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price',1000 );
function add_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $key => $value ) {
        //$value['data']->{'data:protected'}->price = str_replace( ',', '', $result->due );
        $value['data']->price = 123;
    }
}

If you are to print the output of the cart you used to get….

Array
(
[8fcc4ae30dc755002867b36a696c644d] => Array
    (
        [product_id] => 8
        [variation_id] => 0
        [variation] => Array
            (
            )

        [quantity] => 1
        [data] => WC_Product_Simple Object
            (
                [id] => 8
                [post] => WP_Post Object
                    (
                        [ID] => 8
                        [post_author] => 1
                        [post_date] => 2017-03-20 14:56:40
                        [post_date_gmt] => 2017-03-20 14:56:40
                        [post_content] => Zippy zip zip zip-line!
                        [post_title] => Zip-Line Canopy Tour - Half Day
                        [post_excerpt] => 
                        [post_status] => publish
                        [comment_status] => open
                        [ping_status] => closed
                        [post_password] => 
                        [post_name] => zip-line-canopy-tour-half-day
                        [to_ping] => 
                        [pinged] => 
                        [post_modified] => 2017-03-20 14:56:40
                        [post_modified_gmt] => 2017-03-20 14:56:40
                        [post_content_filtered] => 
                        [post_parent] => 0
                        [guid] => http://localhost:81/site/?post_type=product&p=8
                        [menu_order] => 0
                        [post_type] => product
                        [post_mime_type] => 
                        [comment_count] => 0
                        [filter] => raw
                    )

                [product_type] => simple
                [shipping_class:protected] => 
                [shipping_class_id:protected] => 0
                [total_stock] => 
                [supports:protected] => Array
                    (
                        [0] => ajax_add_to_cart
                    )

                [sold_individually] => no
                [price] => 292.02
                [stock_status] => instock
                [manage_stock] => no
                [tax_status] => none
            )

        [line_total] => 292.02
        [line_tax] => 0
        [line_subtotal] => 292.02
        [line_subtotal_tax] => 0
        [line_tax_data] => Array
            (
                [total] => Array
                    (
                    )

                [subtotal] => Array
                    (
                    )

            )

    )

)

But now everything is protected… like so…

Array
(
[f285a86bca6a36eeed98d8e204392442] => Array
    (
        [product_id] => 258
        [variation_id] => 0
        [variation] => Array
            (
            )

        [quantity] => 1
        [line_total] => 1
        [line_subtotal] => 1
        [line_tax] => 0
        [line_subtotal_tax] => 0
        [line_tax_data] => Array
            (
                [total] => Array
                    (
                    )

                [subtotal] => Array
                    (
                    )

            )

        [data] => WC_Product_Simple Object
            (
                [object_type:protected] => product
                [post_type:protected] => product
                [cache_group:protected] => products
                [data:protected] => Array
                    (
                        [name] => zip
                        [slug] => zip
                        [date_created] => WC_DateTime Object
                            (
                                [utc_offset:protected] => 0
                                [date] => 2017-04-12 20:51:02
                                [timezone_type] => 1
                                [timezone] => +00:00
                            )

                        [date_modified] => WC_DateTime Object
                            (
                                [utc_offset:protected] => 0
                                [date] => 2017-04-12 20:51:02
                                [timezone_type] => 1
                                [timezone] => +00:00
                            )

                        [status] => publish
                        [featured] => 
                        [catalog_visibility] => visible
                        [description] => 
                        [short_description] => 
                        [sku] => 
                        [price] => 1
                        [regular_price] => 1
                        [sale_price] => 
                        [date_on_sale_from] => 
                        [date_on_sale_to] => 
                        [total_sales] => 0
                        [tax_status] => taxable
                        [tax_class] => 
                        [manage_stock] => 
                        [stock_quantity] => 
                        [stock_status] => instock
                        [backorders] => no
                        [sold_individually] => 
                        [weight] => 
                        [length] => 
                        [width] => 
                        [height] => 
                        [upsell_ids] => Array
                            (
                            )

                        [cross_sell_ids] => Array
                            (
                            )

                        [parent_id] => 0
                        [reviews_allowed] => 1
                        [purchase_note] => 
                        [attributes] => Array
                            (
                            )

                        [default_attributes] => Array
                            (
                            )

                        [menu_order] => 0
                        [virtual] => 
                        [downloadable] => 
                        [category_ids] => Array
                            (
                            )

                        [tag_ids] => Array
                            (
                            )

                        [shipping_class_id] => 0
                        [downloads] => Array
                            (
                            )

                        [image_id] => 
                        [gallery_image_ids] => Array
                            (
                            )

                        [download_limit] => -1
                        [download_expiry] => -1
                        [rating_counts] => Array
                            (
                            )

                        [average_rating] => 0
                        [review_count] => 0
                    )

                [supports:protected] => Array
                    (
                        [0] => ajax_add_to_cart
                    )

                [id:protected] => 258
                [changes:protected] => Array
                    (
                    )

                [object_read:protected] => 1
                [extra_data:protected] => Array
                    (
                    )

                [default_data:protected] => Array
                    (
                        [name] => 
                        [slug] => 
                        [date_created] => 
                        [date_modified] => 
                        [status] => 
                        [featured] => 
                        [catalog_visibility] => visible
                        [description] => 
                        [short_description] => 
                        [sku] => 
                        [price] => 
                        [regular_price] => 
                        [sale_price] => 
                        [date_on_sale_from] => 
                        [date_on_sale_to] => 
                        [total_sales] => 0
                        [tax_status] => taxable
                        [tax_class] => 
                        [manage_stock] => 
                        [stock_quantity] => 
                        [stock_status] => instock
                        [backorders] => no
                        [sold_individually] => 
                        [weight] => 
                        [length] => 
                        [width] => 
                        [height] => 
                        [upsell_ids] => Array
                            (
                            )

                        [cross_sell_ids] => Array
                            (
                            )

                        [parent_id] => 0
                        [reviews_allowed] => 1
                        [purchase_note] => 
                        [attributes] => Array
                            (
                            )

                        [default_attributes] => Array
                            (
                            )

                        [menu_order] => 0
                        [virtual] => 
                        [downloadable] => 
                        [category_ids] => Array
                            (
                            )

                        [tag_ids] => Array
                            (
                            )

                        [shipping_class_id] => 0
                        [downloads] => Array
                            (
                            )

                        [image_id] => 
                        [gallery_image_ids] => Array
                            (
                            )

                        [download_limit] => -1
                        [download_expiry] => -1
                        [rating_counts] => Array
                            (
                            )

                        [average_rating] => 0
                        [review_count] => 0
                    )

                [data_store:protected] => WC_Data_Store Object
                    (
                        [instance:WC_Data_Store:private] => WC_Product_Data_Store_CPT Object
                            (
                                [internal_meta_keys:protected] => Array
                                    (
                                        [0] => _visibility
                                        [1] => _sku
                                        [2] => _price
                                        [3] => _regular_price
                                        [4] => _sale_price
                                        [5] => _sale_price_dates_from
                                        [6] => _sale_price_dates_to
                                        [7] => total_sales
                                        [8] => _tax_status
                                        [9] => _tax_class
                                        [10] => _manage_stock
                                        [11] => _stock
                                        [12] => _stock_status
                                        [13] => _backorders
                                        [14] => _sold_individually
                                        [15] => _weight
                                        [16] => _length
                                        [17] => _width
                                        [18] => _height
                                        [19] => _upsell_ids
                                        [20] => _crosssell_ids
                                        [21] => _purchase_note
                                        [22] => _default_attributes
                                        [23] => _product_attributes
                                        [24] => _virtual
                                        [25] => _downloadable
                                        [26] => _featured
                                        [27] => _downloadable_files
                                        [28] => _wc_rating_count
                                        [29] => _wc_average_rating
                                        [30] => _wc_review_count
                                        [31] => _variation_description
                                        [32] => _thumbnail_id
                                        [33] => _file_paths
                                        [34] => _product_image_gallery
                                        [35] => _product_version
                                        [36] => _wp_old_slug
                                        [37] => _edit_last
                                        [38] => _edit_lock
                                    )

                                [extra_data_saved:protected] => 
                                [updated_props:protected] => Array
                                    (
                                    )

                                [meta_type:protected] => post
                                [object_id_field_for_meta:protected] => 
                            )

                        [stores:WC_Data_Store:private] => Array
                            (
                                [coupon] => WC_Coupon_Data_Store_CPT
                                [customer] => WC_Customer_Data_Store
                                [customer-download] => WC_Customer_Download_Data_Store
                                [customer-session] => WC_Customer_Data_Store_Session
                                [order] => WC_Order_Data_Store_CPT
                                [order-refund] => WC_Order_Refund_Data_Store_CPT
                                [order-item] => WC_Order_Item_Data_Store
                                [order-item-coupon] => WC_Order_Item_Coupon_Data_Store
                                [order-item-fee] => WC_Order_Item_Fee_Data_Store
                                [order-item-product] => WC_Order_Item_Product_Data_Store
                                [order-item-shipping] => WC_Order_Item_Shipping_Data_Store
                                [order-item-tax] => WC_Order_Item_Tax_Data_Store
                                [payment-token] => WC_Payment_Token_Data_Store
                                [product] => WC_Product_Data_Store_CPT
                                [product-grouped] => WC_Product_Grouped_Data_Store_CPT
                                [product-variable] => WC_Product_Variable_Data_Store_CPT
                                [product-variation] => WC_Product_Variation_Data_Store_CPT
                                [shipping-zone] => WC_Shipping_Zone_Data_Store
                            )

                        [current_class_name:WC_Data_Store:private] => WC_Product_Data_Store_CPT
                        [object_type:WC_Data_Store:private] => product-simple
                    )

                [meta_data:protected] => 
                [price] => 226.48
            )

    )

)

Anyone find a fix for this? I am sure i am not the only one that is having this issue.

thanks!

Read more here: WooCommerce 3.0 can no longer modify cart item price inside woocommerce_before_calculate_totals

Leave a Reply

Your email address will not be published. Required fields are marked *