Download & Extend

Allow commerce_cart_add_to_cart_form() to edit line items

Project:Drupal Commerce
Version:7.x-1.x-dev
Component:Cart
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review

Issue Summary

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.

Comments

#1

Assigned to:Anonymous» 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?

#2

Assigned to:BenStallings» Anonymous

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!

#3

Category:feature request» support request

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.

#4

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

#5

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

#6

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

#7

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
AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-7.patch21.69 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).View details | Re-test

#8

Status:active» needs review

#9

Category:support request» bug report

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.

#10

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
AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-10.patch22.85 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,553 pass(es).View details | Re-test

#11

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.

#12

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

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-12.patch24.22 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).View details | Re-test

#13

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

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-13.patch24.3 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).View details | Re-test

#14

Assigned to:Anonymous» helior

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

#15

Tracking.

#16

Assigned to:helior» Anonymous

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.

AttachmentSizeStatusTest resultOperations
line-item-edit-1211278-16.patch26.54 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).View details | Re-test

#17

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

AttachmentSizeStatusTest resultOperations
line-item-edit-1211278-17.patch25.21 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).View details | Re-test

#18

Oh, and we'd need tests

#19

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

#20

updated -17 to work with the latest dev

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-20.patch25.23 KBIdleFAILED: [[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 details | Re-test

#21

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

#22

Status:needs review» needs work

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

#23

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

#24

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

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-24.patch25.33 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,589 pass(es).View details | Re-test

#25

Status:needs work» needs review

#26

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?

#27

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

#28

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

#29

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

#30

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

#31

Great, thanks for the review Mike.

#32

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

#33

#24 is also working for me.

#34

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?

#35

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

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-35.patch24.95 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,570 pass(es).View details | Re-test

#36

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).
AttachmentSizeStatusTest resultOperations
Error-image-file-field.png19.64 KBIgnoredNoneNone

#37

AttachmentSizeStatusTest resultOperations
Error-image-file-field.png19.64 KBIgnoredNoneNone

#38

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

#39

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.

#40

Attaching the file might actually help.

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-40.patch22.73 KBIdleFAILED: [[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 details | Re-test

#41

Status:needs review» needs work

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

#42

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

AttachmentSizeStatusTest resultOperations
commerce_cart-edit-cart-lines-1211278-42.patch118.79 KBIdlePASSED: [[SimpleTest]]: [MySQL] 3,557 pass(es).View details | Re-test

#43

Status:needs work» needs review

#44

Category:bug report» feature request

#45

Not working for me against 7.x-1.7.

#46

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.