欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

overriding ubercart - add to cart button 超越增加到购物车按钮的方法

shiping1 的头像
rupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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

shareimprove this question
 add comment

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. } };

shareimprove this answer
 
  
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 '12 at 18:07
  
can you post a screenshot of the form array output ? We can't code blindly :) –  Ayesh K Sep 9 '12 at 18:13
  
here is the link. it is too long for a screenshot:) test.biome(dot)sk/sampon-proti-vypadavaniu-vlasov-revita –  loparr Sep 9 '12 at 18:19
  
see the update pls –  Ayesh K Sep 9 '12 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 '12 at 18:55
add comment

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');     


来自 http://drupal.stackexchange.com/questions/42162/overriding-ubercart-add-to-cart-button
普通分类: