Provide an alter hook in commerce_cart_add_to_cart_form() to allow the Line Item to be modified.

It might be necessary for some other module to decide what Line Item type to use in the Add to Cart form. At the moment it is determined by the Product Reference field display settings.

Example: one product display might reference products with different product types. These different product types might require different line items. There is currently no way to cater for different Line Item types in one Add to Cart form.

Other solutions investigated:

  • The Line Item, $line_item, passed to commerce_cart_add_to_cart_form() gets created in commerce_cart_field_formatter_view() and gets modified in commerce_cart_field_attach_view_alter(). Neither of those functions get called when doing an AJAX refresh as a result of changing product attributes on the form. Therefore we cannot use an alter hook in either of those functions.
  • hook_forms() allows us to define a wrapper callback that can alter the arguments passed to commerce_cart_add_to_cart_form(). However, hook_forms() does not work when adding another callback linked to the "commerce_cart_add_to_cart_form_" form ID pattern because that specific form ID pattern is already used in commerce_cart_forms(). When assigning more than one callback to a specific form ID it produces an error while attempting to call an array of callbacks instead of the expected string with one callback name.
CommentFileSizeAuthor
#1 commerce-hook-1799376-0.patch1.36 KBmalberts

Comments

malberts’s picture

Status: Active » Needs review
StatusFileSize
new1.36 KB
agileadam’s picture

Patch applied cleanly to 7.x-1.3.

malberts’s picture

Status: Needs review » Closed (works as designed)

I don't need this hook any more. The effort required to recalculate some variables that occur later in commerce_cart_add_to_cart_form() was more than the effort required to rebuild the line item in a form_alter.

See #1798006-17: Add to Cart Line Item type should be determined by product type or product instance.

agileadam’s picture

Thanks for the update malberts. I've re-patched using #17 as described in your comment #3 above... no longer need this commerce hook. Phew.