Tax is missing in order programmly created order

I create a function that will create an order via the API.
Everything works fine, except that the product-tax and shipping-tax are missing in the order.

Hereby the function I wrote for creating the order.

function create_order($my_data) {

    global $woocommerce;

    $customer = $my_data['customer'];
    $delivery = $my_data['delivery'];
    $items    = $my_data['products'];
    $shipping = $my_data['shipping'];

    $shipping_address = $my_data['shipping_address'];
    $billing_address  = $my_data['billing_address'];        

    $order = wc_create_order();

    $order->set_address($billing_address,  'billing');
    $order->set_address($shipping_address, 'shipping');

    // Add shipping method
    foreach ($woocommerce->shipping->load_shipping_methods() as $shipping_method) {
        if (strcasecmp($shipping_method->id, $shipping['id']) == 0) {
            $shipping['method_title']   = $shipping['name'];
            $shipping['total']          = $shipping['cost'];

            $rate = new WC_Shipping_Rate(   $shipping_method->id,
                                            isset($shipping['method_title']) ? $shipping['method_title']    : '',
                                            isset($shipping['total'])        ? floatval($shipping['total']) : 0,
                                            array(),
                                            $shipping_method->id);
            $order->add_shipping($rate);
            break;
        }
    }

    // Add payment method
    $gateways = new WC_Payment_Gateways();
    $all_gateways = $gateways->get_available_payment_gateways();

    foreach ($all_gateways as $gateway) {
        if ($gateway->id == strtolower ('My_Gateway')) {
            $selected_gateway = $gateway;
            break;
        }
    }
    $order->set_payment_method($selected_gateway);


    // Add items
    foreach ($items as $key => $item){
        $order->add_product( get_product( $item['item_id']), $item['quantity'] );
    }

    $order->calculate_taxes();
    $order->calculate_totals();
}


function create_order($my_data) {

    global $woocommerce;

    $customer = $my_data['customer'];
    $delivery = $my_data['delivery'];
    $items    = $my_data['products'];
    $shipping = $my_data['shipping'];

    $shipping_address = $my_data['shipping_address'];
    $billing_address  = $my_data['billing_address'];        

    $order = wc_create_order();

    $order->set_address($billing_address,  'billing');
    $order->set_address($shipping_address, 'shipping');

    // Add shipping method
    foreach ($woocommerce->shipping->load_shipping_methods() as $shipping_method) {
        if (strcasecmp($shipping_method->id, $shipping['id']) == 0) {
            $shipping['method_title']   = $shipping['name'];
            $shipping['total']          = $shipping['cost'];

            $rate = new WC_Shipping_Rate(   $shipping_method->id,
                                            isset($shipping['method_title']) ? $shipping['method_title']    : '',
                                            isset($shipping['total'])        ? floatval($shipping['total']) : 0,
                                            array(),
                                            $shipping_method->id);
            $order->add_shipping($rate);
            break;
        }
    }

    // Add payment method
    $gateways = new WC_Payment_Gateways();
    $all_gateways = $gateways->get_available_payment_gateways();

    foreach ($all_gateways as $gateway) {
        if ($gateway->id == strtolower ('My_Gateway')) {
            $selected_gateway = $gateway;
            break;
        }
    }
    $order->set_payment_method($selected_gateway);


    // Add items
    foreach ($items as $key => $item){
        $order->add_product( get_product( $item['item_id']), $item['quantity'] );
    }

    $order->calculate_taxes();
    $order->calculate_totals();
}

Read more here: Tax is missing in order programmly created order

Leave a Reply

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