如何在woocommerce中为每个项目获得一个订单

时间:2017-01-07 17:37:34

标签: woocommerce hook

我希望每个订单(订单)收到一个订单。 例如: 如果我们收到三个订单的订单,那么admin-> WooCommerce->订单将为每个商品显示三个不同的订单。

有什么想法吗?

...谢谢

1 个答案:

答案 0 :(得分:0)

请在您的子主题function.php中使用此代码。我希望这段代码能帮助你实现目标。

注意:我建议您先在本地设置中使用此代码。

add_action(' template_redirect',' wc_custom_redirect_after_purchase');

function wc_custom_redirect_after_purchase(){

global $wp,$wpdb;

if(!empty($wp->query_vars['order-received'])){

    $order = new WC_Order($wp->query_vars['order-received']);

    $arrayCount = array();

    $productIDArray = array();

    $productQtyArray = array();

    foreach($order->get_items() as $originalOrderItem){

            createNewOrderAndRecordPayment($wp->query_vars['order-

接收'],$ originalOrderItem [' item_meta'] [' _product_id']

[0],$ originalOrderItem [' item_meta'] [' _qty'] [0]);

    }       

    wp_redirect( '/thank-you/' );

    exit;
}

}

function createNewOrderAndRecordPayment($ orderID,$ productID,$ qty){

global $wpdb;

global $woocommerce;

$original_order_id = $orderID;

$original_order = new WC_Order($original_order_id);

$currentUser = wp_get_current_user();

$order_data =  array(

    'post_type'     => 'shop_order',

    'post_status'   => 'publish',

    'ping_status'   => 'closed',

    'post_author'   => $currentUser->ID, 

    'post_excerpt' => $original_order->customer_message, 

    'post_password' => uniqid( 'order_' )   // Protects the post just in case

);

$order_id = wp_insert_post( $order_data, true );

if ( is_wp_error( $order_id ) ){

    $msg = "Unable to create order:" . $order_id->get_error_message();;

    throw new Exception( $msg );

} else {

    $order = new WC_Order($order_id);

    update_post_meta( $order_id, '_order_shipping', 

get_post_meta($ original_order_id,' _order_shipping',true));

    update_post_meta( $order_id, '_order_ip_value', get_client_ip());

    update_post_meta( $order_id, '_order_discount', 

get_post_meta($ original_order_id,' _order_discount',true));

    update_post_meta( $order_id, '_cart_discount', 

get_post_meta($ original_order_id,' _cart_discount',true));

    update_post_meta( $order_id, '_order_tax', 

get_post_meta($ original_order_id,' _order_tax',true));

    update_post_meta( $order_id, '_order_shipping_tax', 

get_post_meta($ original_order_id,' _order_shipping_tax',true));

    if($orderLoop == 1){

        update_post_meta( $order_id, '_order_total', 

get_post_meta($ original_order_id,' _order_total',true));

    }

    update_post_meta( $order_id, '_order_key',  'wc_' . 

apply_filters(' woocommerce_generate_order_key',uniqid(' order _')));

    update_post_meta( $order_id, '_customer_user', 

get_post_meta($ original_order_id,' _customer_user',true));

    update_post_meta( $order_id, '_order_currency', 

get_post_meta($ original_order_id,' _order_currency',true));

    if($orderLoop == 1){

        update_post_meta( $order_id,

' _prices_include_tax',get_post_meta($ original_order_id,

' _prices_include_tax',true));

    }

    update_post_meta( $order_id, 

' _customer_ip_address',get_post_meta($ original_order_id,

' _customer_ip_address',true));

    update_post_meta( $order_id, 

' _customer_user_agent',get_post_meta($ original_order_id,

' _customer_user_agent',true));

    update_post_meta( $order_id, '_billing_city', 

get_post_meta($ original_order_id,' _billing_city',true));

    update_post_meta( $order_id, '_billing_state',  

get_post_meta($ original_order_id,' _billing_state',true));

    update_post_meta( $order_id, '_billing_postcode', 

get_post_meta($ original_order_id,' _billing_postcode',true));

    update_post_meta( $order_id, '_billing_email', 

get_post_meta($ original_order_id,' _billing_email',true));

    update_post_meta( $order_id, 

' _billing_phone',get_post_meta($ original_order_id,' _billing_phone',true));

    update_post_meta( $order_id, 

' _billing_address_1',get_post_meta($ original_order_id,' _billing_address_1',

真));

    update_post_meta( $order_id, 

' _billing_address_2',get_post_meta($ original_order_id,' _billing_address_2',

真));         update_post_meta($ order_id,

' _billing_country',get_post_meta($ original_order_id,' _billing_country',

真));

    update_post_meta( $order_id, 

' _billing_first_name',get_post_meta($ original_order_id,

' _billing_first_name',true));

    update_post_meta( $order_id, 

' _billing_last_name',get_post_meta($ original_order_id,' _billing_last_name',true));         update_post_meta($ order_id,

' _billing_company',get_post_meta($ original_order_id,' _billing_company',

真));         update_post_meta($ order_id,

' _shipping_country',get_post_meta($ original_order_id,' _shipping_country',

真));         update_post_meta($ order_id,

' _shipping_first_name',get_post_meta($ original_order_id,

' _shipping_first_name',true));

    update_post_meta( $order_id, 

' _shipping_last_name',get_post_meta($ original_order_id,

' _shipping_last_name',true));

    update_post_meta( $order_id, 

' _shipping_company',get_post_meta($ original_order_id,' _shipping_company',

真));

    update_post_meta( $order_id,

' _shipping_address_1',get_post_meta($ original_order_id,

' _shipping_address_1',true));

    update_post_meta( $order_id, 

' _shipping_address_2',get_post_meta($ original_order_id,

' _shipping_address_2',true));

    update_post_meta( $order_id, '_shipping_city', 

get_post_meta($ original_order_id,' _shipping_city',true));

    update_post_meta( $order_id, 

' _shipping_state',get_post_meta($ original_order_id,' _shipping_state',

真));         update_post_meta($ order_id,

' _shipping_postcode',get_post_meta($ original_order_id,' _shipping_postcode',

真));         update_post_meta($ order_id,' _order_delivery_date',$ deliveryDate);

    $itemName = get_the_title($productID);

    $quantity = $qty;

    $lineTotal = $originalOrderItem['line_total'];

    $lineTax = $originalOrderItem['line_tax'];

    $productID = $productID; 

    $item_id = wc_add_order_item( $order_id, array(

        'order_item_name'       => $itemName,

        'order_item_type'       => 'line_item'

    ) );

    wc_add_order_item_meta( $item_id, '_qty', $quantity );

    wc_add_order_item_meta( $item_id, '_product_id', $productID );

    wc_add_order_item_meta( $item_id, '_line_subtotal', 

wc_format_decimal($ lineTotal));

    wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( 

$ lineTotal));

    wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( '0' ) );
    wc_add_order_item_meta( $item_id, '_line_subtotal_tax', 

wc_format_decimal(' 0'));

    $original_order_shipping_items = 

$ original_order-> get_items('航运&#39);

    foreach ( $original_order_shipping_items as 

$ original_order_shipping_item){

        $ShippingItemID = wc_add_order_item( $order_id, array(

            'order_item_name'       => 

$ original_order_shipping_item ['名称&#39],

            'order_item_type'       => 'shipping'

        ) );

        if ( $ShippingItemID ) {

            wc_add_order_item_meta( $ShippingItemID, 'method_id', 

$ original_order_shipping_item [' method_id']);

            wc_add_order_item_meta( $ShippingItemID, 'cost', 

wc_format_decimal($ original_order_shipping_item ['费用']));

        }

    }

    update_post_meta( $order_id, '_payment_method', 

    get_post_meta($original_order_id, '_payment_method', true) );

    update_post_meta( $order_id, '_payment_method_title',   

get_post_meta($ original_order_id,' _payment_method_title',true));

    update_post_meta( $order->id, 'Transaction ID',     

get_post_meta($original_order_id, 'Transaction ID', true) );

    $order->payment_complete();

    $order->update_status('processing');
}

return $order_id;

}