2

I have a Drupal Commerce project setup on top of D7-22. I've overridden the template file for my product display (node--product-display.tpl.php), and have managed to render the product fields, as well as the Add-to-cart form with all of the attributes. I know that <?php print render($content)?> will actually render the <form> for the attributes and Add-to-Cart button, but I would like to customize the HTML layout of the form and add some other elements to it. For example:

  1. I'd like to position the Add to Wishlist button next to the Add to Cart button
  2. I'd like to add some HTML next to one of the attribute fields that is rendered (the Size select box).

Using the Devel module, I was able to pick up fields like product:commerce_price and product:sku, but none of the attributes or buttons within the actual <form>. I'm currently using jQuery to inject HTML into the form...but it's a hack am not all that comfortable with pushing into production.

Is there any way in my tpl.php file to print out the form attributes, quantity textbox and buttons individually?

Cheers!

1

Here's a good start on your first form alter for add to cart:

function MODULENAME_form_alter(&$form, &$form_state, $form_id){
  // Add to cart form overrides
  if(substr($form_id, 0, 30) == 'commerce_cart_add_to_cart_form'){
    kpr($form);
  }
}
0

To edit the "add to cart" form you can use hook_form_alter() or edit the function commerce_cart_add_to_cart_form() in commerce_cart module. In there you can add a second submit button called "wishlist" and add the HTML to the attribute widget. The commerce_cart_add_to_cart_form() function is quite long but it should be doable.

  • thanks for the response. I've had a look through commerce_cart_add_to_cart_form() and it is a bit overwhelming. I'll look through the functions and see if there's a way to achieve my desired output. Wouldn't want to do this in the event of the Drupal Commerce module itself being updated. – Shalan Jul 29 '13 at 9:28
  • I've never attempted a hook_form_alter() before, but I guess now's as good a time as any to start. Do you have any tips for me? – Shalan Jul 29 '13 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.