Hi !
I'm starting using Drupal Commerce and i'm facing a problem.
I'm developing an assurance website.
My problem: the user can add only one item in his cart. Cause he can't subscribe to several offers simultaneously.
So how can i do that in drupal commerce ?
For the moment i've created a new module called "custom_cart" which implements this hook :
function custom_cart_commerce_order_presave($order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Build an array of product line item IDs.
$line_item_ids = array();
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
if ($line_item_wrapper->type->value() == 'product') {
$line_item_ids[] = $line_item_wrapper->line_item_id->value();
$last_line_item_id = $line_item_wrapper->line_item_id->value();
}
}
// Delete each line item one by one from the order. This is done this way
// instead of unsetting each as we find it to ensure that changing delta
// values don't prevent an item from being removed from the order.
foreach ($line_item_ids as $line_item_id) {
if($line_item_id != $last_line_item_id) {
$order = commerce_cart_order_product_line_item_delete($order, $line_item_id, TRUE);
}
}
}
This code is inspired from the function "commerce_cart_order_empty($order)". I've only added a condition to save the last line.
But ! When i use that code, I have this issue :
Notice : Undefined property: stdClass::$is_new dans CommerceOrderEntityController->save() (ligne 122 dans C:\xampp\htdocs\assur4pattes\sites\all\modules\commerce\modules\order\includes\commerce_order.controller.inc).
Notice : Undefined property: stdClass::$is_new dans CommerceOrderEntityController->save() (ligne 142 dans C:\xampp\htdocs\assur4pattes\sites\all\modules\commerce\modules\order\includes\commerce_order.controller.inc).
Notice : Undefined property: stdClass::$is_new dans CommerceOrderEntityController->save() (ligne 148 dans C:\xampp\htdocs\assur4pattes\sites\all\modules\commerce\modules\order\includes\commerce_order.controller.inc).
Am I completely taking it in the wrong way ?
thanks !
Comments
Comment #1
rszrama commentedCan you build this using Rules? There are events pertaining to adding items to the cart, and it should be possible for you to delete any existing line items in the order before adding the new one... that could be a bit of overkill, and it would certainly help if there was a Rules action for emptying the cart. I'll have to add that...
Comment #2
Flowster commentedThanks a lot !
I'll try to add this rule on my own ;).
I'm starting using Drupal since last week for an internship in a French web agency, it's still a bit confusing for me right now, but I'm not gonna giving up !
Comment #3
Flowster commentedAnd there it is !
I've created a module named custom_cart in the commerce modules.
And created the following files :
commerce_custom_cart.rules.inc
commerce_custom_cart.rules_defaults.inc
I've then activate the rule in the configuration page and it works ! (okay I may have had some issues but it finally works ;-) ).
Is it all good this time ?
Comment #4
Flowster commentedComment #5
rszrama commentedExcellent. I've modified the code and added it straight to the Cart module. It won't conflict with your custom module, but you can convert over to the new action if you want.
Comment #6
Flowster commentedOk, thanks !
My first (but not last) contribution to Drupal.
One last question.
I have corrupted my database with orders that points to line item objects that doesn't exist anymore.
What's the simplest way to clean the database without touching the products ?
Comment #7
rszrama commentedJust start truncating tables. ^_^
You'll need to truncate the order and order revisions tables and then the tables of order fields and their revisions. Last, make sure you clear your cache when you're done. There isn't really a simple way, though...
Comment #9
Weaver commentedIs there a way to clear the cart and add item to cart in php (in a custom module) using API's?
Is there any commerce API documentation? (and yes, I have googled extensively thankyou)
Comment #10
rszrama commentedWe're working on setting up an API subsite of DC.org using the same API module powering api.d.o. In the meantime, check out the Specification handbook on DC.org, as it contains articles on various aspects of the APIs.
Comment #11
thomjjames commentedHi,
This seems like an old issue now but wanted to add something I think would be super useful, a rules action to either:
1. Block an item from being added to cart (not sure that's possible?) OR
2. Remove all items of a certain type or SKU from the order
Senario I have now is a site which sells subscriptions & other products, like Flowster only one subscription can be added per cart, but other products have no limit so I can't wipe the whole order.
Wonder if this might be better fit for a contrib module?
Cheers
Tom
Comment #12
rszrama commentedYou can achieve that purely through Rules, even if it is a little redundant to remove a line item you've just added in the case of point 1. For point 2, you'd simply have to loop over the order's line items and pass them one by one to a rules component that checks the line item type / referenced product and deletes it if necessary.
Comment #13
nithinkolekar commentedI am also looking for same feature but couldn't find in rules section.Is it still available with cart module(v 7.x-1.8) or removed?
Comment #14
Marko B commentedAdd a rule:
Event: before add product to cart,
Action: remove all products from order.
Existing products are removed and only the latest selected product is added to the cart.
Comment #15
bsandor commentedI used following code for achieving it:
Comment #16
roball commentedThank you bsandor for your suggestion. Using a solution based on that code is the best for me, since it allows the greatest flexibility. I am now using the following code that clears the existing cart before adding a product to it if contains a line item of a certain type (in this case "registration"):
Note that if you want that code to also work with registrations added to the cart via the Registration Commerce module, the patch from #2452345: hook_commerce_cart_product_prepare and hook_commerce_cart_product_add never invoked must be applied to to
registration_commerce.module.