0

I want to update the product price of each product ( variation currently) based on custom meta fields(created using woocommerce_add_cart_item_data ).

add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3);
function add_custom_field_data($cart_item_data, $product_id, $variation_id)
{
    $dimensionLength = (float) sanitize_text_field($_POST['dimensionLength']);
    $dimensionWidth = (float) sanitize_text_field($_POST['dimensionWidth']);
    $customTotalCharge = (float) sanitize_text_field($_POST['customTotalCharge']);
    $product = wc_get_product($product_id); // The WC_Product Object
    if ($product->product_type=='variable') {
        $new_price =  $customTotalCharge;
        $cart_item_data['custom_data'][$variation_id]['extra_charge'] = (float) $customTotalCharge;
        $cart_item_data['custom_data'][$variation_id]['dimensionLength'] = (float) $dimensionLength;
        $cart_item_data['custom_data'][$variation_id]['dimensionWidth'] = (float) $dimensionWidth;

    }
    return $cart_item_data;
}

Total Price is correct but the price of each product on this page is the original price.

In previous pages, i used different hooks to change the price at different locations.

inrsaurabh

Note : I used these hooks to update the different place

add_action('woocommerce_before_variations_form', 'addCustomDeimensionAction', 5);
add_action( 'woocommerce_calculate_totals', 'extra_price_add_custom_price', 9999, 1 );
add_action('woocommerce_calculate_totals', 'add_custom_price', 10, 1);
add_action('woocommerce_after_calculate_totals', 'woocommerce_after_calculate_totals', 30);
add_action('woocommerce_checkout_create_order_line_item', 'add_text_to_order_items', 20, 4);

above image is from : http://localhost/wordpress/checkout/order-received/183/?key=wc_order_5b5ac9d9d6807

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.