0

I am altering ubercart "uc_product_add_to_cart_form" and adding an ajax button "calculate price":

  $form['price_calculation']['calculate'] = array(
      '#type' => 'button',
      '#value' => t('Prepočítať'),
      '#ajax' => array(
          'callback' => 'activit_calculate_price_ajax',
          'wrapper' => 'price-calculation-value',
          'effect' => 'fade',
          'method' => 'replace'
      ),
  );

This button is just replacing some text info about price based on filled attributes.

Problem is that it also calls submit handler and it adds product to cart.

In case of my own custom submit handler I could verify what button is triggering submit handler, something like if ($form_state['triggering_element']['#value'] == 'calculate') {}. But this is not a custom submit handler. It is default submit handler of altered ubercart form.

Is there any other way to prevent submit handler when ajax button is clicked?


UDATE - I explored ubercart submit handler and commented all the code and added dsm($form_state);

function uc_product_add_to_cart_form_submit($form, &$form_state) {

  dsm($form_state);
  /*
  $form_state['redirect'] =

      uc_cart_add_item(
          $form_state['values']['nid'],
          $form_state['values']['qty'],
          module_invoke_all(
              'uc_add_to_cart_data',
              $form_state['values']
          ),
          NULL,
          variable_get('uc_cart_add_item_msg', TRUE)
      );
  */
}

Weird thing is that $form state shows that triggering element is 'add-to-cart' even if I click only 'calculate price' button.

  • 1
    Form API has #executes_submit_callback I haven't used it though, just remembered seeing it. – sareed May 18 '16 at 13:17
  • Looks like good tip, but it does nothing, submit handler is being still triggered – Incredible May 18 '16 at 13:35
  • Did you try to make the calculate button a submit #type and your submit to something like '#submit' => array('your_submit_function'). This should override the default submit functions and allow you to do your price calculations and return them in $form_state. – sareed May 18 '16 at 15:06
  • I have tried '#submit' => array(''), but no success. – Incredible May 18 '16 at 16:06
  • What's your ajax callback look like? – sareed May 18 '16 at 17:07

Your Answer

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

Browse other questions tagged or ask your own question.