Categories
Drupal Development Resources

Drupal 7 Ubercart 3 Disable cart change quantity field

There may be some instances where you may not want your users to change their quantity on the cart page. An example may be where you want to limit an item to one per order. There are many ways to limit in the product’s node page, but the cart page presents a loophole where the user may edit their quantity.

Replace with MY_MODULE with the name of your own custom module.

Replace CONTENT_TYPE_NAME with the machine name of the content type of the product. Usually this will be “product” unless you defined your own custom product class.

function MY_MODULE_form_alter(&$form, &$form_state, $form_id){
    switch($form_id) {
        case 'uc_cart_view_form':
            $items = uc_cart_get_contents();
            for($i = 0; $i < count($items); $i++) {
            if(isset($form['items'][$i]['#entity']->type) && $form['items'][$i]['#entity']->type == 'CONTENT_TYPE_NAME') {
            $form['items'][$i]['qty']['#disabled'] = true;
        }
    }
    break; 
} 

Alternatively you may set the quantity limit here as well with:

$form['items'][$i]['qty']['#default_value'] = 1;

2 replies on “Drupal 7 Ubercart 3 Disable cart change quantity field”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.