Last active
October 30, 2015 16:35
-
-
Save joshmiller83/ac701dbdde52f46aacd9 to your computer and use it in GitHub Desktop.
Change Add to Cart on hook form alter when price is zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_alter() | |
*/ | |
function MODULE_form_alter(&$form, &$form_state, $form_id) { | |
// Avoid bundle cart forms and other forms | |
if (strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0) { | |
// Use the price calculation system | |
$price = commerce_product_calculate_sell_price($form_state['default_product']); | |
// Some products don't have a price, like bundles. In this case we get "NULL". | |
if ($price !== NULL) { | |
// if price is exactly zero | |
// Note that the amount is in minor units, $1 == 100 and returned as a string, thus, intval() | |
if (intval($price['amount']) === 0) { | |
// replace button with text | |
unset($form['submit']); | |
$form['submit']['#markup'] = '<p class="call-for-pricing">Call for pricing</p>'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment