Update value of total and shipment woocommerce checkout

Viewed 12

Hello everyone I am using the "woocommerce_cart_calculate_fees" hook to add the shipping cost to the products.

This would be an example code:

function preordermodifyprice( $cart ) {
    $cartcount = WC()->cart->get_cart_contents_count();
    if ( $cartcount >= 1 ) {
    $shipping = (WC()->cart->shipping_total + WC()->cart->shipping_tax_total);
    $divideshipping = $shipping; //($shipping / $cartcount);
    foreach ( $cart->get_cart() as $item ) {
        if( get_post_meta( $item['product_id'], '_wc_pre_orders_enabled', true ) ) {
            $adjustprice = $item['data']->get_price() + $divideshipping;
            $item['data']->set_price( $adjustprice );
        }
    }
    $cart->shipping_total = 1;
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'preordermodifyprice', 20, 2 );

As you can see, I update the price of the products that meet the conditions with shipping and at the end I put the value of 1 to the value of shipping costs.

The price of the products change however the total and the shipping cost does not change.

This is fixed by changing the hook From: woocommerce_cart_calculate_fees To: woocommerce_before_calculate_totals

The problem with the woocommerce_before_calculate_totals hook is that I still don't get the shipping cost value

How could I recalculate the value with the modified prices? with the current hook woocommerce_cart_calculate_fees or something similar?

0 Answers
Related