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.

Files: 
CommentFileSizeAuthor
#56 line-items-edit-in-cart-1211278-56.patch23.84 KBkepford
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]
#55 line-items-edit-in-cart-1211278-55.patch23.93 KBkepford
FAILED: [[SimpleTest]]: [MySQL] Invalid PHP syntax in sites/default/modules/commerce/modules/cart/commerce_cart.module.
[ View ]
#52 line-items-edit-in-cart.patch24.95 KBMarc Angles
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch line-items-edit-in-cart.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#48 commerce_cart-edit-cart-lines-1211278-48.patch22.79 KBakalata
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]
#42 commerce_cart-edit-cart-lines-1211278-42.patch118.79 KBakalata
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]
#40 commerce_cart-edit-cart-lines-1211278-40.patch22.73 KBakalata
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-40.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#37 Error-image-file-field.png19.64 KBtribsinpa
#36 Error-image-file-field.png19.64 KBtribsinpa
#35 commerce_cart-edit-cart-lines-1211278-35.patch24.95 KBkepford
PASSED: [[SimpleTest]]: [MySQL] 3,570 pass(es).
[ View ]
#24 commerce_cart-edit-cart-lines-1211278-24.patch25.33 KBrecrit
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-24.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#20 commerce_cart-edit-cart-lines-1211278-20.patch25.23 KBrecrit
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-20.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#17 line-item-edit-1211278-17.patch25.21 KBpcambra
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]
#16 line-item-edit-1211278-16.patch26.54 KBhelior
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]
#13 commerce_cart-edit-cart-lines-1211278-13.patch24.3 KBrecrit
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]
#12 commerce_cart-edit-cart-lines-1211278-12.patch24.22 KBdpolant
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]
#10 commerce_cart-edit-cart-lines-1211278-10.patch22.85 KBrecrit
PASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).
[ View ]
#7 commerce_cart-edit-cart-lines-1211278-7.patch21.69 KBrecrit
PASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).
[ View ]

Comments

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?

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!

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.

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

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).

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

StatusFileSize
new21.69 KB
PASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).
[ View ]

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

Status:Active» Needs review

Category:support» bug

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

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

That probably should be handled by access control.

StatusFileSize
new22.85 KB
PASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).
[ View ]

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

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.

StatusFileSize
new24.22 KB
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

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

StatusFileSize
new24.3 KB
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

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

Assigned:Unassigned» helior

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

Issue tags:+1.3 review

Tracking.

Assigned:helior» Unassigned
StatusFileSize
new26.54 KB
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

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.

StatusFileSize
new25.21 KB
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

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

Oh, and we'd need tests

  • 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.

StatusFileSize
new25.23 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-20.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

updated -17 to work with the latest dev

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.

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

StatusFileSize
new25.33 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-24.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

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

Status:Needs work» Needs review

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?

@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.

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

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

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

Great, thanks for the review Mike.

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

#24 is also working for me.

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?

StatusFileSize
new24.95 KB
PASSED: [[SimpleTest]]: [MySQL] 3,570 pass(es).
[ View ]

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

StatusFileSize
new19.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).

StatusFileSize
new19.64 KB

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);

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.

StatusFileSize
new22.73 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch commerce_cart-edit-cart-lines-1211278-40.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

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.

StatusFileSize
new118.79 KB
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]

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).

Status:Needs work» Needs review

Category:bug» feature
Issue tags:-1.3 review

Not working for me against 7.x-1.7.

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.

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

StatusFileSize
new22.79 KB
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]

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.

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"

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 ?

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.

StatusFileSize
new24.95 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch line-items-edit-in-cart.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

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.

#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.

StatusFileSize
new23.93 KB
FAILED: [[SimpleTest]]: [MySQL] Invalid PHP syntax in sites/default/modules/commerce/modules/cart/commerce_cart.module.
[ View ]

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

Status:Needs work» Needs review
StatusFileSize
new23.84 KB
PASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).
[ View ]

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

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 ?

@Marc Angles it's working perfectly for me.

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.

Confirming #56 to work for 1.8

confirmed #56 works for 1.8 (dev).

Issue summary:View changes

#56 works great! Time for RTBC?

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.

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.

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

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

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

This would be marked fixed if it was. : )

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.