2

I try to override add to cart button with hook form alter in custom module with this function but I got an error Fatal error: fatal flex scanner internal error end of buffer missed in. What can be the problem?

function uc_button_form_alter($form_id, &$form_state, $node) {
  if ($form_id == 'edit-submit-' . $node->nid) {
    $form['submit']['#attributes']=  array(
      'class' => 'node-add-to-cart',
      'xxx' => 'xxx',
    );
  }
};

Thank you

2 Answers 2

3

In D7, below is my solution for different product classes.

/* Overrides the Add to Cart form text*/
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 =='uc_recurring_subscription')){
   $form['actions']['submit']['#value'] = t('Subscribe');     
}

if (($form_id == 'uc_product_add_to_cart_form_'.$node) and ($form['node']['#value']->type =='credit')){
   $form['actions']['submit']['#value'] = t('Buy');     
}

For regular product or just one product class, this should work.

/* Overrides the Add to Cart form text*/
if ( !empty($form['nid']) ) {
  $node = $form['nid']['#value'];
}
else {
  $node = 0;
}
if (($form_id == 'uc_product_add_to_cart_form_'.$node){
   $form['actions']['submit']['#value'] = t('Buy now');     
}
1

Your function's arguments are wrong. See hook_form_alter docs.

So basically you need to rewrite your function as follows.

First, grab the correct form ID.form_alter functions don't have a $node but in some forms, full node object form is in $form. So use the following code. You will now see at least 2 message on your messages area with form ID and array explanation of $form.

function uc_button_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);
  drupal_set_message('<pre>'.print_r($form, 1).'</pre>');
};

Now, check if the node ID you are looking for is available in the $form array. (in most cases, it's in $form['#node']). Now you can change the function to the real code.

function uc_button_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node']->nid) && $form_id == 'edit-submit-' . $form['#node']->nid) {
    $form['submit']['#attributes']['class'] = 'node-add-to-cart';
    $form['submit']['#attributes']['xxx'] = 'xxx'; // we don't want  to clear existing values.
  }
};

However, even this implementation is not necessary. You can make use of default classes with tricky nesting if you only want to add a class to the button.

Update after 3rd comment: Try this:

function uc_button_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['nid']['#value']) && $form_id == 'edit-submit-' . $form['nid']['#value']) {
    $form['submit']['#attributes']['class'] = 'node-add-to-cart';
    $form['submit']['#attributes']['xxx'] = 'xxx'; // we don't want  to clear existing values.
  }
};
4
  • Thank you for quick answer. I tried your code in custom module but it is not working. I use D6. I know that function uc_product_add_to_cart_form is the function I want to override. My aim is to add some additional code inside button's code to track clicks, since I am going to use ajax cart. I tried also a variation of a snippet I found here just to see if it works pastebin.com/btbuELMM function xxx_uc_catalog_buy_it_now_form_alter(&$form, &$form_state) { $form['submit']['#value']= t('Yes i want one'); } none from above works
    – loparr
    Sep 9, 2012 at 18:07
  • can you post a screenshot of the form array output ? We can't code blindly :)
    – AKS
    Sep 9, 2012 at 18:13
  • see the update pls
    – AKS
    Sep 9, 2012 at 18:40
  • No change. To override all buttons I use function acquia_prosper_button($element) put inside template.php. I tried to remove it in case it fights but the html code is not changed at all.
    – loparr
    Sep 9, 2012 at 18:55

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.