Discussions

a separate basket for a different type of product

I want have two shopping carts in my site. One cart for products of type "A", second - for products of type "B"
I know it is possible by Commerce internal design.

for the first i created two modules (could be one, but that's not important right now).
first module adds custom Line Item Type into system - "photoprint"
second module adds custom Order Type - "photoprint"

But I have no good api skills and I have a few questions

The first one: for now i need to write simple hook or write an rule that much better: When user adds to cart the product of type A - it goes to standart shopping cart. But if user adds the cart the product of type "B" - it must go to shopping cart of type "photoprint" (order type "photoprint"). If there is no shopping cart of that type - it must create new order of type "photoprint" with status "cart" and then add lineitems to that order. If cart already exists - just add products to that cart.

Can you help me with that?

Posted: Oct 3, 2013

Comments

prorokxp on October 3, 2013

I want have two shopping carts in my site. One cart for products of type "A", second - for products of type "B" I know it is possible by Commerce internal design (shopping cart = order with status "cart").

for the first i created two modules (could be one, but that's not important right now). first module adds custom Line Item Type into system - "photoprint" second module adds custom Order Type - "photoprint"

But I have no good api skills and I have a few questions

The first one: for now i need to write simple hook or write an rule that much better: When user adds to cart the product of type A - it goes to standart shopping cart. But if user adds the cart the product of type "B" - it must go to shopping cart of type "photoprint" (order type "photoprint"). If there is no shopping cart of that type - it must create new order of type "photoprint" with status "cart" and then add lineitems to that order. If cart already exists - just add products to that cart.

Can you help me with that?

so .. I have the second type of product is added to the cart with VBO. as it turned out, built-in commands "Add product to cart" does not allow to select Line Item Type to add product. And it is necessary. So I decided to write code. After extensive readings of the card's source code and forums on the internet I got this:

global $user;
$uid = 0;
if (!empty($user->uid)) {
   $uid = $user->uid;
}



$order_type = "photoprint_order";

$orders = commerce_order_load_multiple(NULL, array("type" => $order_type, "status" => "cart"));

if (count($orders)>0) {
   $order = array_pop($orders);
} else {
   $order = commerce_cart_order_new($uid, $order_type);
   // Save the order to get its ID.
   commerce_order_save($order);
}

$product = commerce_product_load($product->product_id);

$data = array
(
"context" => array
(
  "product_ids" => array((integer)$product->product_id),
  "add_to_cart_combine" => FALSE,
  "show_single_product_attributes" => FALSE,
  "display_path" => "node/70",
  "entity" => array
  (
   "entity_type" => "node",
   "entity_id" => "70",
   "product_reference_field_name" => "field_product_reference",
   ),
  ),
);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id, $data, 'photoprint_line_item');
commerce_line_item_save($line_item);


// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('photoprint_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;


// Save the order again to update its line item reference field.
commerce_order_save($order);

unfortunately ... not working .. rather works only partially. database table record commerce_line_item appear. all data is correct. but the shopping cart is empty .. And if I add the product directly from product page (using Product reference wiget, but not VBO+rules, which fires up my code), then the item appears on the shopping cart .. Interestingly, the table records commerce_line_item for products that are visible in the cart, is no different from the records of those products, which were added to by code .. symbol to symbol. I did it on purpose, to make sure the problem is not that the data is different and somewhere in the data errors

in general i am looking for a guru-commerce, which will help me .. I seem to be quite close to that to realize multicart feature. pity that i cant find modules that implement it. when the system itself allows it.. ehh