Hi all.. I am new to drupal framework. I installed the commerce file module for my e-commerce website. After installed the line items in cart could not be combine that is when i am adding same product multiple times it should increase the quantity but it does not perform like this it should displayed separate line items..i want to know what is the reason for that. Please help me..

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

iyyappan.govind’s picture

Title: Add to cart restrict » Commerce file does not combine cart line items
iyyappan.govind’s picture

function hook_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
$form_state['line_item']->data['context']['add_to_cart_combine'] = TRUE;
return $form;
}

The above code will combine the cart line items while your using the commerce file.

andrebonfanti’s picture

torgosPizza’s picture

I think this logic is handled in Commerce License now. Make sure you have the latest version. Here is the function as it currently exists:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Prevents the combining of license line items on the Add to Cart form.
 * Prevents the changing of the quantity.
 */
function commerce_license_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  $licensable_line_item_type = in_array($form_state['line_item']->type, commerce_license_line_item_types());
  $has_enabled_products = isset($form_state['default_product']);
  if ($licensable_line_item_type && $has_enabled_products) {
    // Check whether the product is licensable, since the line item might be
    // able to hold both licensable and non-licensable products.
    if (in_array($form_state['default_product']->type, commerce_license_product_types())) {
      $form_state['line_item']->data['context']['add_to_cart_combine'] = FALSE;
      $form['quantity']['#access'] = FALSE;
    }
  }
}

So as long as you have your license settings configured, it should correctly handle combining of line items.

swara05’s picture

Hello @iyyappan govind,

I am also facing the same issue regarding the combine same line items in to cart. As u suggested a form alter method its not working in my case. Is there any alternate to solve this?

If yes the suggestions are welcome.

lukasss’s picture

#2 working for me

iyyappan.govind’s picture

Hi @swara05,

Please tell me what are you trying to do?

Actually you have a settings in your manage display of "Add to cart" to combine the line items. If your using the commerce file module then you need use the #2 solution.Because the commerce file module will reset the add_to_cart_combine to false.

Thanks,

AdamPS’s picture

Title: Commerce file does not combine cart line items » Commerce license/file does not combine cart line items
Project: Commerce File » Commerce License
Category: Support request » Bug report
Priority: Critical » Normal

Changing the module because the relevant code seems to be in commerce license. Updating other issue fields: this seems to be a bug and setting priority according to the handbook recommendations.

#4 Refers to the relevant code in commerce license, but I'm not sure I agree with the conclusion in that comment. This code sets combine to FALSE, and the code comment says "Prevents the combining of license line items on the Add to Cart form." I don't understand why there is code to prevent combine.

The visitor to the site can easily accidentally click "add to cart" twice. It seems to me better to combine these items for clarity on displaying the cart. What's more, once they are combined it is possible to write a rule that sets the quantity back to 1 for sites where multiple licenses are not useful.

The solution in #2 is a workaround, but not really a fix.

Please could we remove the code that is preventing combine?

AdamPS’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev

I think beware with the hook from #2 - it sets combine to TRUE always, regardless of the settings on manage display, and on code in other modules. For some people that may be fine, others may have a different product type that shouldn't combine.

I'll post a patch that removes the line of code that is causing the problem.

AdamPS’s picture

Status: Active » Needs review
FileSize
1.05 KB
swara05’s picture

Hello @iyyappan govind,

Thanks for your reply.

Actually my issue was related to only "Add to cart" to combine the line items & not related to the commerce file using in cart. But problem is solved now, although in manage display of "Add to cart" combine the line items is enabled 'Commerce Price Attribute' module giving the problem in my case just disabled it and all is working fine now.

dmsmidt’s picture

Category: Bug report » Feature request
Priority: Normal » Major
FileSize
4.35 KB

AdamPS, thank you for the patch. But I think we can't just fix it this easily.
It changes the default behavior of the module, while there is the possibility that a shop owner wants to sell/give a separate license per item.

And probably if you want to combine the items in the cart you also want to be able to change the quantity (these two options are normally inseparable).

Therefore I propose this patch, that give the option to change the behavior per product type. Note: you will have to enable the combining and quantity change on the settings form with this patch.

dmsmidt’s picture

AdamPS’s picture

@dmsmidt Thanks very much for a quick and helpful response.

I see your point about changing the default behaviour.

However I think your patch as it stands would not allow the option that I need for my site (of course others may be different, but I think it is a fairly common case), where it makes no sense for a user to have more that one copy of any given license.

I am looking for can't change quantity, but do combine in cart. The reason for this is that if someone accidentally adds to cart twice, if they have combine I can easily write a rule that sets the quantity back to one. With separate line items, it seems rather harder to detect duplicates.

How do you feel about making it separate options on your settings form for "quantity" and "combine"??

====

As an aside, my ideal case would be:

  • After license has been added to cart, add to cart button changes to message "in cart".
  • After license has been bought, add to cart button changes to message "bought" or disappears entirely.

If you have any ideas how to do it I would certainly be interested.

dmsmidt’s picture

Title: Commerce license/file does not combine cart line items » Commerce license does not combine line items and quantity can't be changed
Status: Needs review » Needs work
FileSize
7.45 KB

@AdamPS, I understand your pain but your approach is till a hacky workaround for a different problem.
And you will end up with unused/orphaned licenses.
You try to fix something after the fact I think. I think you should write some logic in a hook_form_commerce_cart_add_to_cart_form_alter().
If you don't have any variants of the product you could simply check if the product is in the order already.
Here is some inspiration: http://agileadam.com/2012/04/drupal-commerce-add-to-cart-form-tweaks-reg...

My previous path also didn't think about these orphaned licenses. Attached my first approach to prevent creating orphaned licenses.
However it is still not 100% foolproof, because I compare by product ID instead of all important line item properties (see code comment).

AdamPS’s picture

OK, thanks for the explanation. So I created a new issue #2559383: Prevent duplicate licenses to request my feature. The link you sent is a good start. I am no longer requesting this feature, but of course others might still be interested.

I don't fully understand your code, so sorry if what I say next makes no sense. But it seems risky if your module/patch is trying to duplicate the calculation made by commerce core whether line items will combine. As your todo indicates it's fairly complex, and what's more that's just the part in core. What if some other module or hook is further altering the situation? Do you definitely need to allocate license objects upon add to cart rather than on checkout completion??

dmsmidt’s picture

@AdamPS

But it seems risky if your module/patch is trying to duplicate the calculation made by commerce core whether line items will combine.

Yes, the patch part of the add to cart form is not production ready, and definitely a hacky approach.
The patch is how I solved it for my project, since I know line-items will be identical per product ID.
The problem is that the line-items comparison process of the commerce module is not a separate method/function which can be reused.

Do you definitely need to allocate license objects upon add to cart rather than on checkout completion?

Good question, I thought about it, but it would need an even bigger module rewrite. And what if the license object needs to be customized during adding to cart? Than yes we need have it at this point, but I currently don't see that possibility by default in the module.

johnrosswvsu’s picture

When there are line items of types shipping, discounts, etc the commerce_license_form_commerce_cart_add_to_cart_form_submit causes an error when submitting items on the cart: EntityMetadataWrapperException: Unknown data property commerce_product.

This is due to the line:
$product_id = $line_item_wrapper->commerce_product->product_id->value();

Not all line items have commerce_product field so this needs to be checked first.

This usually happens when you add items to your cart, attempt to checkout until shipping, and then goes back to cart to add some more items. Adding more items once discounts and shipping are added creates this error.

johnrosswvsu’s picture