As a follow-up to #1033050: Support customizable products via the Add to Cart form, we now have everything in place to use commerce_cart_add_to_cart_form() as an edit form for existing cart line items. It just requires a few changes to the submit handler and the ability for the Add to Cart form display formatter to check to see if there is a "combinable" product already in the shopping cart that the form should be used to edit. The only usability challenge with automatically using the Add to Cart form as an edit form is providing visual indication that the item already exists in the user's cart.

See #1033050-10: Support customizable products via the Add to Cart form for more details.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

BenStallings’s picture

Assigned: Unassigned » BenStallings

I am highly motivated to get this working by January, and I should have the time to devote to it, but I'm not yet up to speed with what needs to be done. Ryan, could you be more specific about what you said above?

  • which submit handler needs to be changed, and what changes does it need?
  • where is the Add to Cart form display formatter found, and where would I find function(s) having to do with combinable products? Wouldn't it be easier to just pass the line item ID to the form?
  • Could the visual indication that we're editing rather than adding be as simple as modifying the node title, or is the Add to Cart form not always displayed within a node?
BenStallings’s picture

Assigned: BenStallings » Unassigned

It looks like assigning this to myself had the effect of causing everyone else to ignore it. Sorry for the misunderstanding. I can't begin work without some answers to the questions above. Thank you in advance!

BenStallings’s picture

Category: feature » support

I'm changing this to a support request in the hopes of getting the information I need to proceed with writing a patch ASAP.

After looking into this some, I think that the submit handler Ryan mentioned is commerce_cart_add_to_cart_form_submit() ... correct?

I think the display formatter is commerce_cart_add_to_cart_form(), but I'm not clear on where/how the $form_state variable gets populated, so I'm not sure how to pre-populate it in a wrapper page, if indeed that needs to be done. The $line_item variable seems straightforward enough... I assume that comes from commerce_line_item_load(), but I'm not clear on whether this is a generic line item that may be added to a cart or a completed line item that has already been added to a cart. That is, I'm not sure where the field values for the specific line item being edited should show up.

Regarding appearing within a node, I'm thinking the edit form should *not* be within a node, since that would make it too easy for the customer to switch products, when the goal here is to edit a product that's already been in the cart.

Am I on the right track? Any guidance would be greatly appreciated.

cimo75’s picture

Hi
I need this myself too, I tried with http://drupal.org/project/editablefields but it doesn t seem to work, probably because D7 is still missing http://drupal.org/project/ajax_load .
Simone

no2e’s picture

commerce_product_attributes offers functionality like this.

It links the products (in the cart) with the line-item id in URL parameter.

· If you visit such a product with the URL parameter, the "Add to cart" button changes to a "Update cart" button.
· If you visit that product without the URL parameter, you see the usual "Add to cart" button and can add the product again (not edit the line item of that product already in cart).

cimo75’s picture

no2e: this is not what I need: what I need is a custom field in the line item that can be edited in the cart itself

recrit’s picture

I believe there are 2 issues here:

  1. Allow commerce_cart_add_to_cart_form() to edit line items (title of this issue)
  2. Add to Cart form display formatter to check to see if there is a "combinable" product already in the shopping cart

The second one should be separate issue since it is more of a UI/UX feature request for the product reference add to cart formatter and the cart line edit capability.

The attached patch is a first pass at the first issue - Allow commerce_cart_add_to_cart_form() to edit line items.

  • Adds a new form "commerce_cart_line_item_edit_form" that uses the "commerce_cart_add_to_cart_form"
  • Updates to commerce_cart_add_to_cart_form to handle existing line items
  • Split commerce_cart_product_add() into smaller functions to allow re-use of line item combining functionality with existing line items
  • Basic UI:
    • Menu callback for line item edit at 'cart/line-items/%commerce_line_item/edit'
    • Views field handler for Cart Line Item Edit link
recrit’s picture

Status: Active » Needs review
Damien Tournoud’s picture

Category: support » bug

I haven't reviewed the patch into detail, but that seems like a good start.

+/** @todo: check that line item order is 'in cart' status? ****/

That probably should be handled by access control.

recrit’s picture

updated patch as follows:

  • Added menu access callback for permission check AND order is cart check
  • Updates views field edit link handler to check menu item access to determine if link should be rendered
  • Added missing quantity setting from form_state values for existing line items
  • Updated commerce_cart_line_item_cart_update() to include a commerce_order_save() to update total calculations
dpolant’s picture

Patch works for me.

On the line item edit form, maybe we should display the price field so that users can see what the price is when they change attributes and line item fields.

dpolant’s picture

Updated patch provides a price field display that updates when you change the attribute widget.

recrit’s picture

re-rolled to worked with latest Commerce 1.x-dev.

rszrama’s picture

Assigned: Unassigned » helior

Assigning to helior for a quick review, as he's been digging through Views code lately. ; )

helior’s picture

Issue tags: +1.3 review

Tracking.

helior’s picture

Assigned: helior » Unassigned
FileSize
26.54 KB

This is neat! Although, has very little to do with Views :P Nonetheless, now that there is a menu item I can finally add Devel and Devel Token support for line items in Commerce Devel! The menu item had to be modified just a little to make it more consistent with the other menu items in order to make that work, though! :)

Overall, the patch looks good to me and everything is functioning as expected. I'll leave it as Needs Review in case anyone else wants to weigh in on this.

pcambra’s picture

I've given this a try and fixed some things.

  • Removed the independent form "commerce_cart_line_item_edit_form" as it was always calling the cart form in the end.
  • Added some token checking to avoid security issues
  • Changed the default product calculation for single products in the cart form.

Not sure why we're using a dedicated function for this: "commerce_cart_line_item_form_menu_item_access" I'd say we should define an independent permission and use the generic commerce access api.

Patch attached

pcambra’s picture

Oh, and we'd need tests

recrit’s picture

  • hook forms for "commerce_cart_line_item_edit_form" provides a wrapper around the add to cart form so that someone could hook_form_alter the "commerce_cart_line_item_edit_form" and know that they are altering the edit form with an existing line item and not the add to cart form with a pseudo line item
  • commerce_cart_line_item_form_menu_item_access - was created to ensure the user could "access checkout" and that the order associated with the line item was in cart status. The line item did not have any entity level permissions to utilize commerce_entity_access(). If there was an "edit own line item" permission then this could be used in some way.
recrit’s picture

updated -17 to work with the latest dev

vasike’s picture

Issue tags: -1.3 review

has no success patching with the last one
#20: commerce_cart-edit-cart-lines-1211278-20.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +1.3 review

The last submitted patch, commerce_cart-edit-cart-lines-1211278-20.patch, failed testing.

recrit’s picture

@vasike - it fails b/c this patch was for the 1.x-dev over a month ago. It will need re-rolled.

recrit’s picture

updated patch that works with 7.x-1.x as of a48de19

recrit’s picture

Status: Needs work » Needs review
vasike’s picture

my test results
- combine : works
- "Cart Line Item Edit link" works

what about Cart Line Item Edit page display? is there a way to control what and how should be displayed on that page
is there anything else to be checked?

recrit’s picture

@vasike:
Currently, its mostly the add to cart form with the price field added to the form array. A custom view mode could be added on the product or line item to customize what fields would show on the "line item: edit form" , then the $show_price and price field in the form array wouldn't be needed.

lsolesen’s picture

I applied the patch to an existing commerce installation. How will I get the cart to show the editable custom line items?

recrit’s picture

You can edit the cart view and then add the field "Cart Line Item Edit link" .

ultimike’s picture

This is a fine addition to the Commerce module. I just installed patch 24 on a site we're building and so far so good.

I'll report back if we see any issues moving forward. I'd love to see this committed to the next version of Commerce.

Thanks,
-mike

rszrama’s picture

Great, thanks for the review Mike.

jasen’s picture

For those who may be interested, I've created a module in a sandbox project that allows editing of all line items after they've been added to the shopping cart:

http://drupal.org/sandbox/jasen/1858212

And here's one that you can use in tandem with the above module to allow administrators to edit the line items in the backend:

http://drupal.org/sandbox/jasen/1858214

Please note that these are sandbox projects, but are working great for me on a few websites...

--
Jasen

akalata’s picture

#24 is also working for me.

SandraVdv’s picture

This patch won't apply to 7.x-1.5...
What functionality will the patch provide exactly? Will it be able to show an attribute field as a select list on the cart page view and change the line item when it's changed?

kepford’s picture

Rerolled patch from #24 against SHA:b18f77832228953a0fd96a1167d91504a7332194. Have not tested it on 7.x-1.5 yet.

tribsinpa’s picture

FileSize
19.64 KB

There is an issue with editing a line item if it has an image/file field attached. Error is thrown on when there is an existing image attached that is not modified. There is no error thrown if all images are removed or if a new "fresh" images are attached.

Using
Drupal 7.19
Commerce 7.x-1.4

    Notice: Trying to get property of non-object in file_field_presave() (line 220 of /home/gopgolf/commerce-test/modules/file/file.field.inc).
    Notice: Undefined property: stdClass::$uri in file_save() (line 570 of /home/gopgolf/commerce-test/includes/file.inc).
    PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1363212658 ) in drupal_write_record() (line 7251 of /home/gopgolf/commerce-test/includes/common.inc).
tribsinpa’s picture

FileSize
19.64 KB
tribsinpa’s picture

I found the issue, in #36. It has to do with the Commerce Product Attributes module. This module adds a submit callback to the commerce_cart_add_to_cart_form that deletes the line item completely when it is "edited". When the line item is deleted it also deletes the managed file from the system. Once the managed file is gone it starts throwing those errors

The fix is the commerce_product_attributes_add_to_cart_form_submit function removes the line item REFERENCE to the order, rather than delete the entire line item. The caveat is that there may be cron function needed that periodically cleans up the orphaned line items.

I altered commerce_products_attributes/commerce_products_attributes.module
lines 256-273

/**
 * Submit callback function for the add to cart function.
 */ 
function commerce_product_attributes_add_to_cart_form_submit($form, &$form_state) {
  $product_id = $form_state['values']['product_id'];
  $product = commerce_product_load($product_id);

  // If the line item passed to the function is new, then
  // use the default handler.
  if (empty($form_state['line_item']->line_item_id)) {
    commerce_cart_add_to_cart_form_submit($form, $form_state);
  }
  else {
    $order = commerce_order_load($form_state['line_item']->order_id);
   
    // Remove the previous line item.
-    commerce_cart_order_product_line_item_delete($order, $form_state['line_item']->line_item_id);
+  commerce_entity_reference_delete($order, 'commerce_line_items', 'line_item_id', $form_state['line_item']->line_item_id);
akalata’s picture

Attached an update of #35 that should work with 7.x-1.5 (patch was applying to commerce_cart.views.inc in the wrong place).

There is some views-related refactoring in commerce_cart.views.inc in 7.x-1.x-dev, but I don't know enough about what's going on to make additional updates just yet.

akalata’s picture

Attaching the file might actually help.

Status: Needs review » Needs work

The last submitted patch, commerce_cart-edit-cart-lines-1211278-40.patch, failed testing.

akalata’s picture

Trying against 7.x-1.x. If you're trying to apply #40, I missed a new file (commerce_cart_handler_field_cart_line_item_link_edit.inc - still figuring out this patching stuff).

akalata’s picture

Status: Needs work » Needs review
pcambra’s picture

Category: bug » feature
Issue tags: -1.3 review
ADrupalUser’s picture

Not working for me against 7.x-1.7.

askibinski’s picture

I tested the patch at #42 against 1.7 and -dev, but I think it's made for an older version? Patch didn't apply cleanly, however I got to the following steps:

I Added the new field 'Cart Line Item Edit link' to the /cart view 'commerce_cart_form'.
After saving the view I got the notices below and the view does not display anymore.

Notice: Undefined index: left_field in views_join->construct() (line 1500 van [...] /sites/all/modules/contrib/views/includes/handlers.inc).
Notice: Undefined index: field in views_join->construct() (line 1501 [...] /sites/all/modules/contrib/views/includes/handlers.inc).

I'm using the latest stable version of Views, Drupal, etc. I'm not very familiar with views handlers but hope this will help. Would be a great patch!

BTW: I might mention that I got 2 products in my cart in the above scenario: one with 3 variations (products) available and one which is always one parent node <-> one product, but the results were the same with just one of the products.

askibinski’s picture

For anyone struggling with this, I found this module, which does mostly what I needed:
http://drupal.org/project/commerce_line_item_cart_form

akalata’s picture

Hey askibinski -- I realized a few weeks ago I didn't have the views stuff working, finally got around to fixing it. Woohoo!

Thanks for sharing the module in #47, but for me I needed an edit page.

Patch below is against 7.x-1.x from 4/30/13.

Marc Angles’s picture

HI, akalata, thanks a lot for this !

Patch #48 applies well on 7.x-1.7.

Unfortunatelly, when I add the "Cart Line Item Edit link" to my view, it asks for confirmation of the relationship and we end up with a "Broken/missing handler"

Marc Angles’s picture

There was a views release soon after the patch was submitted https://drupal.org/node/1965242. Could it be the cause of the broken/missing handler bug ?

Marc Angles’s picture

Here is some debug messages

_views_create_handler - loading handler handler(handler type: field) failed: class commerce_cart_handler_field_cart_line_item_link_edit could not be loaded. Verify the class file has been registered in the corresponding .info-file (files[]).

Missing handler: commerce_line_item cart_line_item_link_edit field

What happen is that the handler does not show at all. I needed to disable cache in views, restart php and memcached, empty the cache. Then the handler appears in the views config and display this message when trying to add it. It is not a test in a brand new drupal installation.

Marc Angles’s picture

Ok, last patch did not create modules/cart/includes/views/handlers/commerce_cart_handler_field_cart_line_item_link_edit.inc

Here is another one.

It is magik :)

Thanks a lot.

kepford’s picture

#52: line-items-edit-in-cart.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, line-items-edit-in-cart.patch, failed testing.

kepford’s picture

Rerolled line-items-edit-in-cart.patch from #52. This time against 1.8.

kepford’s picture

Status: Needs work » Needs review
FileSize
23.84 KB

Let's try that again. Rerolled line-items-edit-in-cart.patch from #52. This time against 1.8.

Marc Angles’s picture

Hmmm... the patch applies.

But the edit form is completely empty. No fields, just the price...

@kepford, do you have good results ? Is this functioning well for you ?

kepford’s picture

@Marc Angles it's working perfectly for me.

Marc Angles’s picture

Ok, it is working now. But on some products I have this Fatal error

Fatal error: Class name must be a valid object or a string in /drupal-7-2013.08.22/includes/common.inc on line 7827

I don't know if this error is caused by this patch. I can confirm it is not caused by this patch.

abelwang’s picture

botris’s picture

Confirming #56 to work for 1.8

darrenmothersele’s picture

confirmed #56 works for 1.8 (dev).

Anybody’s picture

Issue summary: View changes

#56 works great! Time for RTBC?

halloffame’s picture

Successfully applied patch #56 against 1.8 and 1.x-dev but nothing happens upon clicking the edit link, it just reloads the cart page.

darrenmothersele’s picture

I had confirmed for 1.8, against a D7 site with commerce modules.

Tried on Commerce Kickstart and found same as comment #64, but didn't have time to look further into what was going on.

darrenmothersele’s picture

Just to confirm, the version without Commerce Kickstart, just the 1.8 commerce module, is working fine with this patch.

mglaman’s picture

#56 applied clean and worked on Commerce 1.8 bundled in Commerce Kickstart.

halloffame’s picture

Is this committed to the latest 7.x-1.9?

rszrama’s picture

This would be marked fixed if it was. : )

pixeldroid’s picture

This doesn't work with Commerce Pricing Attributes. Items with price attributes are not added to cart. System shows "Product added to cart" but the cart is empty.

Marc Angles’s picture

#56 applies correctly on 1.9.

And it works fine.

Status: Needs review » Needs work

The last submitted patch, 56: line-items-edit-in-cart-1211278-56.patch, failed testing.

Marc Angles’s picture

I applied the patch on 7.x-1.10

Now, anonymous cannot see the line-items anymore.
When logged-in they see it without problem.

rszrama’s picture

Wow, I completely forgot about this issue and the patches it had. I had a requirement for a client to allow editing of attributes in the cart form, so I created a custom Views form field handler that basically builds the Add to Cart form using the line item data as expected and embeds the resulting attribute widgets in the form.

I still need to ensure it reloads the full View when a line item changes, but otherwise it's golden. One thing it doesn't have is line item fields, but those could be added in a like manner using a separate field handler.

I'm inclined to go this route over the full edit form approach in the patches above, as it's a much smaller footprint and something that anyone can easily add to their Views without disrupting any existing site. Anyone wanna review?

serundeputy’s picture

Applied patch: https://www.drupal.org/files/issues/1211278-75.edit_attributes_in_cart.p...
I added an attribute field to my commerce cart view: `/admin/structure/views/view/commerce_cart_form/edit`, but received an error when trying to visit the shopping cart.

Notice: Undefined index: #value in commerce_cart_handler_field_edit_attributes->views_form() (line 68 of /Users/geoff/Sites/commerce/sites/all/modules/contrib/commerce/modules/cart/includes/views/handlers/commerce_cart_handler_field_edit_attributes.inc).
Notice: Undefined index: #value in commerce_cart_handler_field_edit_attributes->views_form() (line 69 of /Users/geoff/Sites/commerce/sites/all/modules/contrib/commerce/modules/cart/includes/views/handlers/commerce_cart_handler_field_edit_attributes.inc).
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 737 of /Users/geoff/Sites/commerce/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

unapplied the patch and I'm able to get back to the shopping cart.

Am I doing it wrong?

~Geoff

serundeputy’s picture

Issue #1211278: Commerce Module
Why:

* Allow editing of product attributes from the cart.

This change addresses the need by:

* Check isset($subform['product_id']['#value']) make sure there is a #value index.

rszrama’s picture

Great lead on the error there - it turns out the reason you saw the error was because your products weren't using attributes but the basic product select list element. In that case, we needed to look in its #default_value property instead of #value. I've updated the code to pull it from there instead and to function if it can't find the product_id element at all.

I've also updated the refresh callback to completely re-render the View when it's called and drop it in as a full replacement. That was the clearest way I could see to update the entire cart form when a line item product changed. It's a bit hacky to unset $_POST variables directly like this, but I just disable what I needed to to ensure the Views form wouldn't be re-processed when we manually rebuilt the View.

Status: Needs review » Needs work

The last submitted patch, 78: 1211278-77-78.interdiff.patch, failed testing.

rszrama’s picture

I just remembered I need to test this with Commerce Stock to make sure it's gravy. In any event, here's a screenie of this sucker in action working both for a product with attributes (including attributes that collide) and a simple product selector:

rszrama’s picture

Status: Needs work » Fixed

Did some additional testing and committing this sucker. Whoop whoop.

  • rszrama committed 1774c5f on 7.x-1.x
    Issue #1211278 by rszrama: add a Views form field handler for editing...
Anybody’s picture

Whao, thank you so much! Great work!! Whoop whoop! ;)

rszrama’s picture

hehe, thanks. : )

To use, you just edit the Shopping Cart Form View and add the attribute widgets field handler. Should be plug and play. File new issues in the queue if you can break it. : P

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

scotty014’s picture

Is this feature still available? Looking at the most recent commerce version I don't see any of the changes from this commit.

rszrama’s picture

Yep, it's there. You just need to edit the View and plug in the new field handler. I haven't written a tutorial for it yet...

scotty014’s picture

I figured it out. Didn't realize I needed the Dev version of the commerce module to get the updated code.

rszrama’s picture

Ahh, yeah, I've been trying to roll a 1.12 for months now and it just keeps slipping. :-/

NotGoddess’s picture

I'd like clarification on just what was committed.
It appears that the patch that was committed doesn't have the same features as the one in #56.
#56 allowed an 'edit' link to go to the line item entity and edit it.
The patch committed #82 seems to allow editing attributes from the cart form.
#56 worked great for me - but the version committed doesn't appear to since I have line items with multiple fields and don't display them individually - I need the separate form.

If I'm just mistaken can you provide some instructions, or do I need to roll a version of #56 that'll work with 1.13?

Thanks

rszrama’s picture

Yeah, what was committed was not a full line item edit form. I did suggest if we need line item fields as opposed to attribute selection widgets to appear in the Cart that a similar Views field handler could be committed.

NotGoddess’s picture

Okay. I re-rolled the line item version against the latest dev.

bunset’s picture

Hello!. I am new applying a patch and I don't get it.
Please. What am I doing bad please? Thanks!

After seeing what is the patch for my commerce version,
I try to apply path #92 over commerce 1.13, then I download the patch file and I put it in directory profiles/commerce_kickstart/modules/contrib/commerce.
And there, I type the command: patch < commerce-line-items-edit-in-cart-1211278-92.patch
but it says this error:
---------------------------------------
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/modules/cart/commerce_cart.info b/modules/cart/commerce_cart.info
|index d7195fc..11ff819 100644
|--- a/modules/cart/commerce_cart.info
|+++ b/modules/cart/commerce_cart.info
--------------------------
File to patch:
-------------------------------------------------

bisonbleu’s picture

@bunset, try with patch -p1 < commerce-line-items-edit-in-cart-1211278-92.patch

bisonbleu’s picture

FileSize
61.58 KB

OK, I'm officially jealous. It's not fair, product types get all the love...

Can we also have Attribute field widgets for line item type fields... please!

I added a List (float) field to the Product line item type in CK2 then added the Commerce Line Item: Attribute field widgets in the cart view but I only see product type attributes (color & size), snif~

Where's my widget?

rszrama’s picture

Is that not already possible with the editable fields module? I suppose it's just not directly integrated with the cart submit.

In any event, that would be a separate feature request. Those aren't attribute fields, just line item fields.

bisonbleu’s picture

@rszrama, thanks for the quick reply. I created a separate feature request: #2735065: Allow commerce_cart_add_to_cart_form() to edit line item fields

iyyappan.govind’s picture

Hi How to do this in Drupal 8?