Woocommerce add cart fee based on another order amount

25 Views Asked by At

I am trying to add a cart fee based on another order amount.

The other order is the parent order in a "order again" process. The ID of this oder is given by the $edited session variable.

The following code works as expected except when I add/modify checkout options (which are fees). The amount of these fees are added to the parent order total, thus total is wrong.

Any idea?

add_action( 'woocommerce_cart_calculate_fees', 'woo_use_edit_order_total', 20, 1 );
   
function woo_use_edit_order_total( $cart ) {
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
     
    $edited = WC()->session->get('edit_order');
    if ( ! empty( $edited ) ) {
        $order = new WC_Order( $edited );
        if ( $order ) {
            $credit = -1 * $order->get_total();
            $cart->add_fee( __( 'Credit', 'divi-ultimate' ), $credit);
        }
    }
}
0

There are 0 best solutions below