1

I am setting up a very simple Drupal commerce site for a few courses with 3 product variations for different price levels. I created a product type "Course Registration" and gave it these payment options:

  • DEP|Deposit
  • EB|Early Bird
  • FULL|Full Price

I have a content type "Course Display". On the bottom of a product display page I get this:

Payment Option
o Deposit
o Early Bird
o Full Price
[Add to Cart]
$250

From what I've been able to figure out the radio buttons and submit button are displayed part of the "Add to Cart form" widget for 'product' field. The price is coming from the 'Product: Price" field. I would like have the price show above the submit button like this:

Payment Option
o Deposit
o Early Bird
o Full Price
$250
[Add to Cart]

I assume this means getting the "Add to Card form" to show the price? I'm at a loss for how to proceed.

1 Answer 1

4

You can add the rendered field into the add to cart form like this:

function mytheme_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  $product = $form_state['default_product'];
  $view_mode = $form_state['context']['view_mode'];
  $form['price'] = field_view_field('commerce_product', $product, 'commerce_price', $view_mode);
}
5
  • Awesome. Thanks so much. Maybe I can start to understand how this works a little. Next I want to add a 'Show Cart' button adjacent to the 'Add to Cart' button. If you could give any help on how to do that or how any of this is working in general I'd find that really useful.
    – tukra
    Mar 8, 2014 at 2:25
  • Sure. You can add a link to the form by adding something like this to the hook I posted: $form['show_cart'] = array('#markup' => l(t('Show Cart'), 'cart'), '#weight' => 99); All we're doing is adding elements to the form's render array after it's been built.
    – gigabates
    Mar 8, 2014 at 2:37
  • Okay this is starting to make more sense. I want a button that acts like an href to '{root}/cart/' $form['show_cart'] = array('#type' => 'button', '#value' => t('View cart'), '#attributes' => array('onclick' => 'window.location.href=\'cart\''), '#weight' => 65, ); but can't figure out how to get rid of the "submit"
    – tukra
    Mar 9, 2014 at 1:55
  • I figured out how to leave it as a link and style it to look like a button. Not that clean though since I have to duplicate the button css in the theme. Is there a better way?
    – tukra
    Mar 9, 2014 at 2:57
  • You could add another submit button with its own submit handler that redirects to the cart page. I think that a link is more semantically correct though is it's not really related to submitting the form.
    – gigabates
    Mar 9, 2014 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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