2

Here is a more detailed explanation:

In Drupal Commerce cart, when a customer sets the quantity of a product from 1 to 5 for example, on 'Update Cart', we'll need 5 individual line items of the same product with quantity 1 each.

I've gotten this far on solving the problem but I'm not sure how to continue:

<?php 
/**
 * Implements hook_entity_update
 */
function mymodule_entity_update($entity, $type){
  if ($type == 'commerce_line_item'){
    $original = intval($entity->original->quantity);
    $updated = intval($entity->quantity);

    if ($original != $updated){
        $new_product = $entity;
        // Create as many individual line items of the same product as needed, save them and add them to the order
    }
  }
}

I've tried to use commerce_line_item_save() to save $new_product, which is in this code snippet should be a clone of the line item. I've tried it with increasing the line item id and save it that way but I just got DB errors.

How can I make this happen in a nice way? Thank you!

  • Possible duplicate: drupal.stackexchange.com/q/97404/633 – Andy Jun 26 '14 at 13:23
  • 1
    I've already did what the accepted answer proposed, which keeps the items seperate when using Add to cart, but it doesn't work when the quantity is updated in the shopping cart. – Bálint Deáki Jun 26 '14 at 13:36
  • 1
    I'm with you. AFAICT this behaviour of the cart quantity form isn't configurable (see the end of commerce_line_item_handler_field_edit_quantity::views_form_submit()). IMHO it would be weird from a UX POV to enter 5 into the quantity textfield of 1 line item, submit, and then see 5 distinct line items with quantity 1. What about having an 'Add 1 more' button? – Andy Jun 26 '14 at 14:59
  • An 'Add 1 more button' sounds a lot better. Even with that I'm still not sure how to create 5 identical line items with quantity 1 out of 1 line item with quantity 5. – Bálint Deáki Jun 27 '14 at 8:20
  • 1
    OK. Is it the case that all products on your site should always show up as separate line items, or is it only some products? – Andy Jun 27 '14 at 9:36
1

It would be possible to create a new views handler based on commerce_line_item_handler_field_edit_quantity that worked the way you want, but IMHO it would be weird from a UX POV to enter 5 into the quantity textfield of 1 line item, submit, and then see 5 distinct line items with quantity 1.

A simple alternative would be to modify the cart view to have something like an Add 1 more button:

  1. Remove the quantity field;
  2. Add a relationship to the product;
  3. Add the product's add to cart form as a field;
  4. Configure the field to not combine products (and you can also modify the text here, eg Add 1 more).

I haven't tested this, but I think it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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