Discussions

Hide "Add to cart" if field exists[SOLVED]

Hi everybody,
i'm building an ecommerce and i've addedd into a variation a field called "Sold out" width radio buttons.

How can i hide the "buy" or "add to cart" button if the product is sold out(sold out field cheked)?

Anybody can help me?
Thanks

Posted: Feb 18, 2013

Comments

cavax on February 18, 2013

i solved with this code made by me:

<?php
function MYTHEME_form_alter(&$form, &$form_state, $form_id){
 
//check if is the form cart
 
if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {
   
//filter only for the products with the variations
   
if (isset($form['product_id']['#value'])) {
     
$product = commerce_product_load($form['product_id']['#value']);
      if(
$product->field_soldout_['und'][0]['value']){
       
$form['submit']['#value'] = t('Sold out');
       
$form['submit']['#disabled'] = TRUE;
       
$form['submit']['#attributes'] = array('class' => array('sold-out'));
      }
    }
  }
}
?>

where "field_soldout_" soldout is my field, hope that can help someone.