如果原始订单中有未交货的产品,则拆分订单并创建新订单 [英] Split an order and create a new order if the original order has products in backorder

查看:48
本文介绍了如果原始订单中有未交货的产品,则拆分订单并创建新订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有自定义订单状态,并且可以正常运行.

I have the custom order status and it works.

在这里,我需要帮助的是遍历订单项,并检查是否有任何待补订单,如果是,则从原始订单中取出待补项目,并创建一个新订单,并为新订单提供状态.

What I need help with here is looping through the order items and checking if any of them are on backorder and if so, take the backorder items out of the original order and create a new order and give the new order the Backorder status.

这个想法是当且仅当原始订单中有待补项目上时,才创建补货.

add_action( 'woocommerce_checkout_order_processed', 'split_order_if_order_has_backorder_products', 10, 1 );
function split_order_if_order_has_backorder_products( $order_id ) {
    if( ! $order_id ) return;

    // get order object
    $order = new WC_Order( $order_id );

    // get order currency
    $currency = $order->get_currency();

    // get order payment method
    $payment_gateway = $order->get_payment_method();

    // get order items = each product in the order
    $items = $order->get_items();

    foreach ( $items as $item ) {       
        $product = wc_get_product( $item['product_id'] );

        if ( $product->is_on_backorder() ) {

        // THIS IS NOT WORKING FOR SOME REASON?
        $backorder = wc_create_order();         
        $backorder->add_product( $product, $item['quantity'] );
        }
    }

    // THIS IS NOT WORKING EITHER = I NEED TO REMOVE THE BACKORDER ITEMS FROM THE ORIGINAL ORDER, RECALCULATE AND SAVE THE NEW TOTAL
    foreach($items as $backorder_product => $item){
        $order->remove_item($backorder_product);
        $order->calculate_totals();
        $order->save();
    }

    $address = array(
        'first_name' => $order->get_billing_first_name(),
        'last_name'  => $order->get_billing_last_name(),
        'email'      => $order->get_billing_email(),
        'phone'      => $order->get_billing_phone(),
        'address_1'  => $order->get_billing_address_1(),
        'address_2'  => $order->get_billing_address_2(),
        'city'       => $order->get_billing_city(),
        'state'      => $order->get_billing_state(),
        'postcode'   => $order->get_billing_postcode(),
        'country'    => $order->get_billing_country()
    );

    $shipping = array(
        'first_name' => $order->get_shipping_first_name(),
        'last_name'  => $order->get_shipping_last_name(),
        'address_1'  => $order->get_shipping_address_1(),
        'address_2'  => $order->get_shipping_address_2(),
        'city'       => $order->get_shipping_city(),
        'state'      => $order->get_shipping_state(),
        'postcode'   => $order->get_shipping_postcode(),
        'country'    => $order->get_shipping_country()
    );

    // Set addresses
    $backorder->set_address( $address, 'billing' );
    $backorder->set_address( $shipping, 'shipping' );

    // set the correct currency and payment gateway
    $backorder->set_currency($currency);
    $backorder->set_payment_method($payment_gateway);

    // calculate totals
    $backorder->calculate_totals();

    // set order note with original ID
    $backorder->add_order_note('Automated Backorder. Created from the original order ID: '.$order->get_id());

    // give the new backorder the correct status
    $backorder->update_status( 'backorder' );
}

推荐答案

下面的代码仅在原始订单中包含有缺货状态的产品时,才与缺货的产品一起创建新订单

The code below ONLY creates a new order with the products that are in backorder, if the original order contains products with backorder status

add_action( 'woocommerce_checkout_order_processed', 'split_order_if_order_has_backorder_products', 10, 1 );
function split_order_if_order_has_backorder_products( $order_id ) {
    if( ! $order_id ) return;

    // get order object
    $order = new WC_Order( $order_id );

    // get order currency
    $currency = $order->get_currency();

    // get order payment method
    $payment_gateway = $order->get_payment_method();

    // get order items = each product in the order
    $items = $order->get_items();

    // check whether order contains back orders
    $check_for_back_orders = false;

    foreach ( $items as $item ) {       
        $product = wc_get_product( $item['product_id'] );

        if ( $product->is_on_backorder() ) {
            // will only be executed once if the order contains back orders
            if ( $check_for_back_orders == false ) {
                $check_for_back_orders = true;

                // create new order with backorders
                $backorder = wc_create_order();
            }

            // add product to 'backorder'
            $backorder->add_product( $product, $item['quantity'] );

            //delete item from original order
            $order->remove_item( $item->get_id() );
        }
    }

    $order->calculate_totals();
    $order->save();

    // If order contains backorders (true)
    if ( $check_for_back_orders ) {
        $address = array(
            'first_name' => $order->get_billing_first_name(),
            'last_name'  => $order->get_billing_last_name(),
            'email'      => $order->get_billing_email(),
            'phone'      => $order->get_billing_phone(),
            'address_1'  => $order->get_billing_address_1(),
            'address_2'  => $order->get_billing_address_2(),
            'city'       => $order->get_billing_city(),
            'state'      => $order->get_billing_state(),
            'postcode'   => $order->get_billing_postcode(),
            'country'    => $order->get_billing_country()
        );

        $shipping = array(
            'first_name' => $order->get_shipping_first_name(),
            'last_name'  => $order->get_shipping_last_name(),
            'address_1'  => $order->get_shipping_address_1(),
            'address_2'  => $order->get_shipping_address_2(),
            'city'       => $order->get_shipping_city(),
            'state'      => $order->get_shipping_state(),
            'postcode'   => $order->get_shipping_postcode(),
            'country'    => $order->get_shipping_country()
        );

        // Set addresses
        $backorder->set_address( $address, 'billing' );
        $backorder->set_address( $shipping, 'shipping' );

        // set the correct currency and payment gateway
        $backorder->set_currency($currency);
        $backorder->set_payment_method($payment_gateway);

        // calculate totals
        $backorder->calculate_totals();

        // set order note with original ID
        $backorder->add_order_note('Automated Backorder. Created from the original order ID: '.$order->get_id());

        // give the new backorder the correct status
        $backorder->update_status( 'backorder' );
    }
}

这篇关于如果原始订单中有未交货的产品,则拆分订单并创建新订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆