I was just on this too recently.
So save the dimensions data using the woocommerce_add_cart_item_data
filter. I see you are only taking in 2 arguments. If this is variable product, you’ll need all 4 params in the filter.
Then we do the pricing override. You can’t use woocommerce_add_to_cart
to do the pricing override as the product data doesn’t get carried over to the session cart. You need to use the woocommerce_before_calculate_totals
hook to iterate through the cart contents and change the product price.
This code is taken directly from this accepted answer: https://stackoverflow.com/questions/43324605/change-cart-item-prices-in-woocommerce-version-3-0
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( 40 );
}
}
To elaborate a little, check out the class-wc-cart.php
‘s add_to_cart()
method to see what is stored each content item. Basically the $value['data']
in the above code is the product you are storing foreach product. You want to check $value['your_custom_info_key']
that you set previously in that item data filter to get your custom info, where your_custom_info_key
should not be any of the following as these are used by WooCommerce itself.
'key' => $cart_item_key,
'product_id' => $product_id,
'variation_id' => $variation_id,
'variation' => $variation,
'quantity' => $quantity,
'data' => $product_data,