Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Add Product To Cart Multiple Times But As Different items

I have some custom code that adds an item to the cart, a variable product. If i add the same product twice it just increases the quantity. I would like it to add the product again as a different item in the cart.

How would i go about doing that? The item is being added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434 and so on.

The system i have developed is a product designer. So i may want to pick the same product but design it in different ways, and then add the same variation to the cart.

Is there also a way to send a unique identifier in the url to split these items up?

I want to do it by using add_to_cart but every time i do that with the variation attributes, the shipping ends up with a bug, it basically can't seem to find shipping 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 that code adds the item to the cart, it causes the following:

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

There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.

It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and has none of these issues. The same if i use a URL, it works fine!

like image 352
Glen Avatar asked Feb 12 '23 17:02

Glen


2 Answers

Try this !

/*
 * @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 );
like image 143
Yannick Armspach Avatar answered May 02 '23 21:05

Yannick Armspach


I can't tell yet if the shipping array issue is related. But, if you take a look at the beginning of the add_to_cart() method 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, what we're seeing here is that the item must be completely unique in order to add it to the cart again. Otherwise, it will just up the quantity of the item already in the cart. So, to get around this, you will want to make the add_to_cart() parameters unique... probably via the final, $cart_item_data array.

like image 42
helgatheviking Avatar answered May 02 '23 22:05

helgatheviking