4
Answers
Vote up!
0
Vote down!

How to alter the Add-to-Cart button for disabled to a link?

Hi,

I want to alter the function of the disabled submit button to be able to get to the contactform when someone clickes on the submit button when the product is disables.

Thanks for the advice in pseudocode : What you'd need to do is alter the Add to Cart form from a custom module, looking for a disabled submit button and re-enabling it with a different message and a new submit handler that redirects to the contact form upon submission.

Using this pseudocode I made this custom module, but it doesn't work.
Can you help me please?

<?php
/**
 * @file
 * Adds a link to the contact page when clicking on the add to cart form of a disabled product
 */
/**
 * Implements commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter().
 */
function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id)
  {
    if(
$form['submit']['#disabled'] == true)
       {
      
$form['submit']['#value'] = t('Please contact us');
      
$form['actions']['submit']['#submit'][] = 'commerce_product_disabled_link_product_form_submit';

       }
   }
/**
 * Implements commerce_product_disabled_link_product_form_submit().
 */
function commerce_product_disabled_link_product_form_submit($form, &$form_state) {
// Set the redirect to the disabled product
$form_state['redirect'] = 'contact';
}
?>

Thanks a lot in advance.

greetings, Martijn

Asked by: Summit
on June 3, 2013

4 Answers

Vote up!
0
Vote down!

With the help of Morne Alberts I got a solution! I would love also to be able to add values to the contactform from the product like SKU, body, but will post that as another question.
This is the endcode:

<?php
/**
 * @file
 * Adds a link to the contact page when clicking on the add to cart form of a disabled product
 */

/**
 * Implements hook_form_FORM_ID_alter(): commerce_cart_add_to_cart_form
 */
function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  if (!empty(
$form['submit']['#attributes']['disabled'])) {
    
$form['contact_link'] = array(
            
'#markup' => l(t('Please contact us'), 'contact'),
       ); 
   
$form['submit']['#value'] = t('Contact for larger amounts');
    unset(
$form['submit']['#attributes']['disabled']);
    unset(
$form['submit']['#validate']);
   
$form['#validate'] = array();
   
$form['#submit'] = array('commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit');
  }
}

/**
 * Add to cart form submit handler.
 */
function commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit($form, &$form_state) {
 
// Set the redirect to the disabled product
  
$form_state['redirect'] = 'contact';
}
?>


The remarks which may help others are:
change:
<code>
$form['submit']['#submit'][] =
'commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit';

to
$form['#submit'] =
array('commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit');

If you look at the bottom of http://drupalcontrib.org/api/drupal/contributions!commerce!modules!cart!...
you can see that the submit handler is placed directly on the form, not
the button. Also, note that [] is removed at the end. If you use [] then it will
trigger your handler in addition to any others, so it will also fire the
original submit. Now you're telling it to only use yours.
Also #submit value must always be an array, even if there's only 1 value.

You also need the following unset($form['submit']['#validate]); otherwise validation fails and
also add: $form['#validate'] = array(); to remove the original validator.

unset($form['submit']['#validate]);
$form['#validate'] = array();

I also needed to replace underneath because in HTML as long as the disabled attribute exists, no matter what the value, then it will be disabled.

  $form['submit']['#attributes']['disabled'] = FALSE;

with
  unset($form['submit']['#attributes']['disabled']);

Other important remarks are:
when implementing a specific form alter you should rather say that
form's name instead of your function name in the comment
- using !empty() in the IF is the same as checking if there is a value
and if the value is TRUE, so you don't have to do 2 IF's
- the contact_link form element is no longer necessary (unless you also
want the link)

$form['contact_link'] = array(
             '#markup' => l(t('Please contact us'), 'contact'),
       ); 

- the second function does not implement a hook, so you can just give
it a comment indicating that it is a submit handler.
- the submit handler uses $form instead of &$form, because you cannot
change the $form after it has been submitted.

Greetings, Martijn

Answer by: Summit
Posted: Jun 10, 2013
Vote up!
0
Vote down!

The problem appears to be that you've correctly altered the value of the button using:

<?php
$form
['submit']['#value'] = t('Please contact us');
?>

But when you add the submit handler to the button, you use an incorrect variable:

<?php
$form
['actions']['submit']
?>

If you get rid of the actions bit, you'll be much closer to a working implementation. Make sure you've cleared your cache if you added this alter hook to an already installed module as well.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Jun 4, 2013

Comments

Hi Ryan,
I tried that and changed to:

$form['submit']['#submit'][] = 'commerce_product_disabled_link_product_form_submit';

But this didn't work, no clickability possible on the button and no altering of text to "Please contact us'.
I can't figure this out, and I think more people will benefit from the fact that you can change the behaviour of disabled products so that customers can still do something with this information!

Do you may be mean http://www.drupalcommerce.org/questions/6815/how-should-i-disabled-add-c... by what you answered on email?

Greetings and much appreciated your reply!
Martijn

- Summit on June 4, 2013

Since the button is disabled, you also need to enable it. Set it's #disabled to FALSE.

- Ryan Szrama on June 4, 2013

Hi Ryan,

I made this, but still not working and also Notice: Undefined index: #disabled in commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(

function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id)
  {
    if($form['submit']['#disabled'] == true)
       {
       $form['submit']['#value'] = t('Please contact us');
       $form['submit']['#disabled'] = false;
       $form['submit']['#submit'][] = 'commerce_product_disabled_link_product_form_submit';

       }
   }
/**
* Implements commerce_product_disabled_link_product_form_submit().
*/
function commerce_product_disabled_link_product_form_submit($form, &$form_state) {
// Set the redirect to the disabled product
$form_state['redirect'] = 'contact';
}
- Summit on June 5, 2013

Hi, I am one little step further...I can alter the text of the button to ''Please contact us' using beneath code, but the redirect is not working and somehow the add to cart button is not triggered to false also as you suggested..

function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
  if($form['submit']['#attributes']['disabled'] == true) {
    debug($form, '$form contents:', TRUE);
$form['submit']['#attributes']['disabled'] = false;  
$form['submit']['#value'] = 'Please contact us';
    $form['submit']['#submit'][] = 'commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit';
   }
}
/**
* Implements commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit().
*/
function commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit($form, &$form_state) {
// Set the redirect to the disabled product
$form_state['redirect'] = 'contact';
}

The debug gives this info

Debug: $form contents::
Array
(
    [#attributes] => Array
        (
            [class] => Array
                (
                    [0] => commerce-add-to-cart
                    [1] => commerce-cart-add-to-cart-form-5826
                )

        )

    [uid] => Array
        (
            [#type] => value
            [#value] => 1
        )

    [submit] => Array
        (
            [#type] => submit
            [#value] => Contact voor grote aantallen
            [#weight] => 15
            [#attributes] => Array
                (
                    [disabled] => disabled
                )

            [#validate] => Array
                (
                    [0] => commerce_cart_add_to_cart_form_disabled_validate
                )

        )

    [#validate] => Array
        (
            [0] => commerce_cart_add_to_cart_form_validate
        )

    [#submit] => Array
        (
            [0] => commerce_cart_add_to_cart_form_submit
        )

Any help much appreciated!

greetings, Martijn

- Summit on June 6, 2013
Vote up!
0
Vote down!

Instead of

<?php
 
$form
['submit']['#submit'][] = 'commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit';
?>

try

<?php
$form
['#submit'] = 'commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit';
?>

The submit handler is set directly on the form, not on the button (look at commerce_cart_add_to_cart_form()).

Also, note that I did not use []. This is because I want only my submit handler to be called, instead of in addition to the others, i.e. the original submit handler.
However, I suspect that you might need to also do

<?php
unset($form['submit']['#validate']);
?>

otherwise validation will fail (I did not test this, BTW).

Having said all that, I think it might be easier just to alter the form by adding a link to the contact page instead of trying to modify/revert the submit button.
Off the top of my head, you would then only have to do the following:

<?php
if($form['submit']['#attributes']['disabled'] == true) {
 
$form['contact_link'] = array(
   
'#value' => l(t('Please contact us'), 'contact'),
  );
}
?>

This will create a new form element that is just a link and then you don't have to worry about doing anything further to the form.
Answer by: malberts
Posted: Jun 8, 2013
Vote up!
0
Vote down!

Hi,
I got rid of the index error by using: if (ISSET($form['submit']['#attributes'])) {
But code still not working, can anyone help please? Trying for hours now!

<?php
/**
 * Implements commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter().
 */
function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
 
  if (ISSET(
$form['submit']['#attributes'])) {
     if(
$form['submit']['#attributes']['disabled'] == TRUE) {
      
/** debug($form, '$form contents:', TRUE); */
   
$form['submit']['#value'] = 'Please contact us';
      
/**
        * Underneath not working... no activation/enabling of Submit button and no triggering of submit handler
        */
       
$form['submit']['#attributes']['disabled'] = FALSE
      
$form['submit']['#submit'][] = 'commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_submit';
     }
  }
}
/**
 * Implements commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_submit().
 */
function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_submit(&$form, &$form_state) {
// Set the redirect to the disabled product
 
drupal_set_message(t('Contact redirect.'));
 
$form_state['redirect'] = 'contact';
}
?>
Answer by: Summit
Posted: Jun 9, 2013