Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

php - Create individual orders for each item in WooCommerce cart

I basically need to be able to create multiple orders for every item in the cart but retain the default checkout process.

To the user, it would look like they are checking out with multiple items but in the backend, it would create multiple orders. I found an article which pretty much relates what I need to do.

I would assume I would need to remove the action that is triggered when clicking the pay now button, preventing the order from being created automatically and create the orders manually. I can access the cart information using the following filter, perhaps this would be a starting point?

add_filter( 'woocommerce_checkout_create_order', 'wp_handle_multiple_orders', 10, 1 );
function wp_handle_multiple_orders( $order ) {
global $woocommerce;
print_r(count(WC()->cart->get_cart()));

// Handle creation of multiple orders

}

Or perhaps I hook into the checkout_process:

add_action( 'woocommerce_checkout_process', 'wp_handle_multiple_orders' ) );

I am aware this process isn't ideal and there are many different things to consider but hopefully, there is an alternative way of achieving this. Any links would be appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This answer contains:

  • Keep only 1 product in the original order, delete other products from this order.
  • Place the removed products (each separately) in a new order.
  • Explanation via comment tags added to the code, the code can easily be expanded further if desired
function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {   
    // Get order currency
    $order_currency = $order->get_currency();

    // Get order payment method
    $order_payment_gateway = $order->get_payment_method();
    
    // Address
    $order_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
    $order_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()
    );
    
    // Counter
    $counter = 1;

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        // From the 2nd product
        if ( $counter >= 2 ) {
            // Get the WC_Product Object
            $product = $item->get_product();
            
            // Get product quantity
            $product_quantity = $item->get_quantity();
            
            // Create new order
            $new_order = wc_create_order();
            
            // Add product to new order
            $new_order->add_product( $product, $product_quantity );
            
            // Set addresses
            $new_order->set_address( $order_address, 'billing' );
            $new_order->set_address( $order_shipping, 'shipping' );

            // Set the correct currency and payment gateway
            $new_order->set_currency( $order_currency );
            $new_order->set_payment_method( $order_payment_gateway );

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

            // Set order note with original ID
            $new_order->add_order_note( 'Automated order. Created from the original order ID: ' . $order_id );
            
            // Set correct status
            $new_order->update_status( 'processing' );

            // Delete product from original order
            $order->remove_item( $item_id );
        }
        
        // Counter = counter + 1
        $counter++;
    }
    
    // Re-calculate & save
    $order->calculate_totals();
    $order->save();
}
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...