Limit Quantity By "Product Type"

Public

Alter Commerce "Add to cart form"

Get raw version
php
  1. function YOUR_MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id, $a1)
  2. {
  3. $form['#submit'][] = 'YOUR_MODULE_form_commerce_cart_add_to_cart_form_submit';
  4. $form['#validate'][] = 'YOUR_MODULE_form_commerce_cart_add_to_cart_form_validate';
  5. }

Add Limitation =)

Get raw version
php
  1. function YOUR_MODULE_form_commerce_cart_add_to_cart_form_submit(&$form, &$form_state)
  2. {
  3. if($line_item = $form_state['line_item']){
  4. $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
  5. if('book_product' === $product->type){
  6. //Limit Quantity to 1
  7. $line_item->quantity = 1;
  8. commerce_line_item_save($line_item);
  9. }
  10. }
  11. }

Or You may deny to add

Get raw version
php
  1. function YOUR_MODULE_form_commerce_cart_add_to_cart_form_validate($form, &$form_state)
  2. {
  3. form_set_error('','Product Already In Cart');
  4. }