0

So I am using hook_form_alter to alter the add-to-cart button for my Ubercart pages. See here:

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ( !empty($form['nid']) ) {
    $node = $form['nid']['#value'];
  }
  else {
    $node = 0;
  }

  if (($form_id == 'uc_product_add_to_cart_form_'.$node) and ($form['node']['#value']->type =='product')){

    hide($form['actions']['submit']);

    $form['button'] = array(
      '#prefix' => '<button type="submit" id="edit-submit-'. $node .'" name="op" class="node-add-to-cart btn btn-cart">',
      '#suffix' => '</button>',
      '#markup' => 'Buy for: ',
      '#weight' => 1,
    );
  }
}

This works great. Except, in my form['button'] array, want to change the button markup to something like: 'Buy for: $10'.

How can I access the node's list and selling price so that I can use it in the markup of the button?

1

The node object is made available through the $form array in $form['node']['#value']. You can find list & sell price through the $form['node']['#value']->list_price & $form['node']['#value']->sell_price properties.

  • Thanks Shawn! It half-works. If my product's price is $10, the output I get on my product node page is: 10.00000, rather than just '$10' as I need. Any ideas? Thanks for your time – caustic Nov 10 '15 at 20:58
  • 1
    The property property is stored as a float. If you want to display it as an integer you can cast it (e.g. (int) $form['node']['#value']->sell_price) or round it 2 decimals (e.g. round($form['node']['#value']->sell_price, 2) ) in the event you'll need fractional pricing. – Shawn Conn Nov 10 '15 at 21:09
  • Thank you Shawn, that worked. I have one more question though... how can I display the store currency with the price, other than just printing a dollar sign? Is there an easy method to show the current currency before that pricing? – caustic Nov 10 '15 at 21:24
  • That I don't know. I've never had to use Ubercart in an international/multi-currency setup. That be better asked as separate question. – Shawn Conn Nov 10 '15 at 22:02

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.