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
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
Since the button is disabled, you also need to enable it. Set it's #disabled to FALSE.
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';
}
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