I have the following code to split an order and create a new order with a custom status.
What I require is to notify the administrator when that new order is generated. So far the notification codes for a custom order have not worked.
The code to divide it that I am using is this:
function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) { // Initialize $check_for_back_orders = false;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Get product
$product = $item->get_product();
// Product is on backorder
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_order = wc_create_order();
}
// Add product to 'backorder' order
$backorder_order->add_product( $product, $item['quantity'] );
// Delete item from original order
$order->remove_item( $item->get_id() );
}
}
// If current order contains backorders, retrieve the necessary data from the existing order and apply it in the new order
if ( $check_for_back_orders ) {
// Recalculate and save original order
$order->calculate_totals();
$order->save();
// Obtain necessary information
// Get address
$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()
);
// Get shipping
$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()
);
// Get order currency
$currency = $order->get_currency();
// Get order payment method
$payment_gateway = $order->get_payment_method();
// Required information has been obtained, assign it to the 'backorder' order
// Set address
$backorder_order->set_address( $address, 'billing' );
$backorder_order->set_address( $shipping, 'shipping' );
// Set the correct currency and payment gateway
$backorder_order->set_currency( $currency );
$backorder_order->set_payment_method( $payment_gateway );
// Calculate totals
$backorder_order->calculate_totals();
// Set order note with original ID
$backorder_order->add_order_note( 'Pedido a confirmar stock automatizado. Creado a partir del ID de pedido original: ' . $order_id );
// Optional: give the new 'backorder' order the correct status
$backorder_order->update_status( 'wc-shipping-progress' );
// $backorder_order->update_status( 'on-hold' );
}
} add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );
The code for the new custom status is this:
// Register New Order Statuses
function wpex_wc_register_post_statuses() {
register_post_status( 'wc-shipping-progress', array(
'label' => _x( 'Confirmar Stock', 'WooCommerce Order status', 'text_domain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Approved (%s)', 'Approved (%s)', 'text_domain' )
) );
}
add_filter( 'init', 'wpex_wc_register_post_statuses' );
// Add New Order Statuses to WooCommerce
function wpex_wc_add_order_statuses( $order_statuses ) {
$order_statuses['wc-shipping-progress'] = _x( 'Confirmar Stock', 'WooCommerce Order status', 'text_domain' );
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wpex_wc_add_order_statuses' );
So far the notification codes for a custom order have not worked.