Woocommerce Add product to cart Several times, but as different items

I have a custom code that adds an item to the cart, a variable product. If I add the same product twice, it just increases the amount. I would like it to add the product again as another item to the cart.

How can i do this? The item is added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434, etc.

The system I have developed is the product developer. Therefore, I can choose the same product, but its design is different, and then add the same variations to the basket.

Is there a way to send a unique ID in the url to separate these items?

I want to do this using add_to_cart, but every time I do it with variation attributes the delivery ends up with an error, it basically can't find delivery methods:

$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));

      

While this code is adding an item to the cart, it calls the following:

Warning: Invalid argument provided by foreach () in / home / **** / public_html / wp-content / plugins / woocommerce / includes / class-wc-shipping.php on line 291

There seems to be no shipping method available. Please double check your address or contact us if you need help.

It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and doesn't have any of these problems. The same, if I use the url it works great!

+3


source to share


2 answers


Try it!



/*
 * @desc Force individual cart item
 */
function force_individual_cart_items( $cart_item_data, $product_id ){

  $unique_cart_item_key = md5( microtime().rand() );
  $cart_item_data['unique_key'] = $unique_cart_item_key;

  return $cart_item_data;

}

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


/*
 * @desc Remove quantity selector in all product type
 */
function remove_all_quantity_fields( $return, $product ) {

    return true;

}

add_filter( 'woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2 );

      

+5


source


I can't tell yet if the problem is with the shipping array. But if you look at the beginning of the method add_to_cart()

in the cart class:

// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id        = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );

// See if this product and its options is already in the cart
$cart_item_key  = $this->find_product_in_cart( $cart_id );

      



Basically, we can see that the item must be completely unique in order to add it to the cart again. Otherwise, the number of items will already be in the basket. So, to get around this, you'll want to make the parameters add_to_cart()

unique ... probably via the final array $cart_item_data

.

0


source







All Articles