Tuesday, October 29, 2013

Commerce Product Bundle: link to product fix

Link to original issue: https://drupal.org/node/1668100.

This issue took me days to find. It just didn't make sense, but once I found the underlying problem, the solution was pretty simple.



At the end of the commerce_product_bundle_add_to_cart_form_submit function, find the following code:
    $response = commerce_product_bundle_add_to_cart(
      $form_state['values']['uid'],
      $product_id,
      $form_state['values']['quantity'],
      $subproducts,
    );
And replace it with the following:
    $response = commerce_product_bundle_add_to_cart(
      $form_state['values']['uid'],
      $product_id,
      $form_state['values']['quantity'],
      $subproducts,
      $form_state
    );
All we did just now was add the $form_state array to the parameter list. Now scroll down a bit and find the following function definition:
function commerce_product_bundle_add_to_cart($uid, $product_id, $quantity, $subproducts, $display_uri = NULL)

Add $form_state before $display_uri. It should look like this:
function commerce_product_bundle_add_to_cart($uid, $product_id, $quantity, $subproducts, $form_state, $display_uri = NULL)

Here is the key part. Scroll down and find the following line:
$line_item = commerce_product_line_item_new($product, $quantity, $order->order_id, $display_uri);

This is where the commerce_display_path should be set, but since the line item data array isn't being passed, it doesn't get populated. Since we now have access to $form_state, we can pass the line item data. Replace the above with the following:
$line_item = commerce_product_line_item_new($product, $quantity, 0, $form_state['line_item']->data, $form_state['line_item']->type);

That's it! The bundled products will now be linked if commerce_display_path is appropriately set up in the view.

No comments:

Post a Comment