这里的目标是循环浏览订单商品,并采用未交货的商品并将其放置在新订单中,并为该新订单赋予状态“ Backorder
”。
我使用的代码给了我两个订单,但是旧订单仍然包含未交货的物料,而新订单中没有任何物料。
关于我要去哪里的任何想法吗?
add_filter( 'woocommerce_register_shop_order_post_statuses', 'register_backorder_status' );
function register_backorder_status( $order_statuses ) {
$order_statuses['wc-backorder'] = array(
'label' => _x( 'Backorder', 'Order status', 'woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'On Backorder', 'Being Packed <span class="count">(%s)</span>', 'woocommerce' ),
);
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'display_backorder_order_status' );
function display_backorder_order_status( $order_statuses ) {
$order_statuses['wc-backorder'] = _x( 'On Backorder', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_filter( 'bulk_actions-edit-shop_order', 'enable_backorder_order_status_bulk' );
function enable_backorder_order_status_bulk( $bulk_actions ) {
$bulk_actions['mark_backorder'] = 'Set as Backorder';
return $bulk_actions;
}
add_filter( 'wc_order_statuses', 'add_backorders_to_order_statuses' );
function add_backorders_to_order_statuses( $order_statuses ) {
$new_order_status = array();
foreach ( $order_statuses as $key => $status ) {
$new_order_status[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_status['wc-backorder'] = 'Backorder';
}
}
return $new_order_status;
}
add_action( 'woocommerce_thankyou', 'split_order_if_order_has_backorder_products', 10, 1 );
function split_order_if_order_has_backorder_products($order_id) {
if ( ! $order_id )
return;
$backorder_status = 'wc-backorder';
$order = wc_get_order( $order_id );
$currency = $order->get_currency();
$paymentMethod = $order->get_payment_method();
$billing_information = 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_information = 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()
);
/* not sure if this is needed or not?
if ( ! $order->has_status('on-hold') )
return;
*/
$backorder_items = array();
foreach ( $order->get_items() as $item ) {
$product = wc_get_product($item['product_id']);
if ($product->is_on_backorder()) {
$backorder_items[] = $item;
}
}
$backorder_args = array (
'customer_id' => $order->get_customer_id(),
'status' => 'wc-backorder',
);
$backorder = wc_create_order($backorder_args);
foreach($backorder_items as $backorder_item){
$backorder->add_item($backorder_item);
}
$backorder->set_currency($currency);
$backorder->set_payment_method($paymentMethod);
$backorder->set_address( $billing_information, 'billing');
$backorder->set_address( $shipping_information, 'shipping');
$backorder->add_order_note('This is the automated backorder, created from the original order ID: '.$order->get_id());
foreach($backorder_items as $item_id => $backorder_item){
$order->remove_item($item_id);
}
$order->calculate_totals();
$order->save();
$backorder->calculate_totals();
$backorder->save();
}