0

I am using Woocommerce plugin with WP and when I add to cart an item and then I add to cart the same item again, at the cart page it's displayed as one item with quantity of two and with one order_id. What I want is to be able to add to cart multiple times the same product and each product to get a unique order_id. Is there a way to do that? Thank you.

0

1 Answer 1

0
function bbloomer_split_product_individual_cart_items( $cart_item_data, $product_id ){

  $unique_cart_item_key = uniqid();

  $cart_item_data['unique_key'] = $unique_cart_item_key;

  return $cart_item_data;

}


add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_split_product_individual_cart_items', 10, 2 );


 

//add_filter( 'woocommerce_is_sold_individually', '__return_true' );

 

 

 

#***************************************************

add_action( 'woocommerce_add_to_cart', 'mai_split_multiple_quantity_products_to_separate_cart_items', 10, 6 );

function mai_split_multiple_quantity_products_to_separate_cart_items( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {

 

    // If product has more than 1 quantity

    if ( $quantity > 1 ) {

 

        // Keep the product but set its quantity to 1

        WC()->cart->set_quantity( $cart_item_key, 1 );

 

        // Run a loop 1 less than the total quantity

        for ( $i = 1; $i <= $quantity -1; $i++ ) {

            /**

             * Set a unique key.

             * This is what actually forces the product into its own cart line item

             */

            $cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );

 

            // Add the product as a new line item with the same variations that were passed

            WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );

        }

 

    } 

 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.