Horje
woocommerce order splitting - Wordpress Solution
I have tried modifying the answer provided in https://stackoverflow.com/questions/59544167/split-a-woocommerce-order-and-create-a-new-order-if-the-original-order-has-produ/59544491#59544491. I am not getting any errors when testing it, but also nothing really happens to my test order. Can someone help me fix this code so that it's actually splitting the order? The idea is that line items with the tag "stock" should get split into their own orders, from the main/parent order. Also the line item should be the deleted from the main/parent order.
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );

function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
// Initialize
$check_for_stock = false;
$taxonomy = 'product_tag';

// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
    // Get product
    $product = $item->get_product();

    // Product has tag 'stock'
    if ( has_term( 'stock', $taxonomy, $product ) ) {
        // Will only be executed once if the order contains line items with tag "stock"
        if ( $check_for_stock == false ) {
            $check_for_stock = true;

            // Create new order with stock items
            $stock_order = wc_create_order();
        }

        // Add product to 'backorder' order
        $stock_order->add_product( $product, $item['quantity'] );
        $stock_order->save();

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

// If current order contains line items with product tag "stock", retrieve the necessary data from the existing order and apply it in the new order
if ( $check_for_stock ) {
    // 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
    $stock_order->set_address( $address, 'billing' );
    $stock_order->set_address( $shipping, 'shipping' );

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

    // Calculate totals
    $stock_order->calculate_totals();

    // Set order note with original ID
    $stock_order->add_order_note( 'Automated "stock" order. Created from the original order ID: ' . $order_id );
    
    // Optional: give the new 'stock' order a new status
    //$stock_order->update_status( 'on-hold' );
}
}


Solution - 1

Which version of woocommerce you are using?


Solution - 2

This below line in your code generates a product object instead of product id.


// Get product
$product = $item->get_product();
For has_term to work, you actually need ProductID which can be obtained as below:
// Get product id
		$item_product_id = $item->get_product_id();

And then you can use your IF condition as follows:

if ( has_term( 'stock', $taxonomy, $item_product_id ) ) {
//rest of your code here
Hope this helps.





Wordpress

Related
Display post loop associated with current category url slug - Wordpress Solution Display post loop associated with current category url slug - Wordpress Solution
L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution
wp query with multiple of same meta keys - Wordpress Solution wp query with multiple of same meta keys - Wordpress Solution
list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution
Create array of hours within wordpress loop - Wordpress Solution Create array of hours within wordpress loop - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
30