Skip to content

Instantly share code, notes, and snippets.

@joshmiller83
Last active October 30, 2015 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmiller83/ac701dbdde52f46aacd9 to your computer and use it in GitHub Desktop.
Save joshmiller83/ac701dbdde52f46aacd9 to your computer and use it in GitHub Desktop.
Change Add to Cart on hook form alter when price is zero
<?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