By default I've Quantity widget: Disabled.
I'm planning to re-enable it for certain product types.
So the feature proposal is to create action which will enable or disable Quantity widget.

Related:
http://drupal.stackexchange.com/questions/123818/how-to-programmatically...

CommentFileSizeAuthor
#6 Screen Shot 2014-07-21 at 11.23.52.png15.21 KBkenorb

Comments

kenorb’s picture

Issue summary: View changes
MarcElbichon’s picture

Is this only for cart view and checkout pages ?
Do you want it too for view product page ?

kenorb’s picture

For Cart view and eventually Checkout.
I was thinking about defining the rule, but there is no action to change the Quantity widget type.

What I've done so far it's workaround.
So I've created TPL file: views-view-field--commerce-cart-form--default--quantity.tpl.php
with the following code:

print foo_quantity_available_for_row($row) ? $output : 1; // Hardcoded '1'.

and the following two functions:

/**
 * Implements hook_views_pre_view().
 */
function foo_views_pre_view(&$view) {
  switch ($view->name) {
    case 'commerce_cart_form': // On cart page
    // case 'commerce_cart_summary': // On summary page
      $view->display['default']->handler->options['fields']['quantity']['field'] = 'edit_quantity'; // Enforce Quantity field from numeric to text field for further changes.
      break;
  }
}

/**
 * Restrict edit of Quantity field based on the product type.
 *
 * Callback from views-view-field--commerce-cart-summary--default--quantity.tpl.php.
 *
 */
function foo_quantity_available_for_row($row) {
  ($line_item = $row->commerce_line_item_field_data_commerce_line_items_line_item_) &&
    ($line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item)) &&
      ($product = $line_item_wrapper->commerce_product->value());

  if ($product) {
    switch ($product->type) {
      case 'product_type_1':
      case 'product_type_2':
        return TRUE; // Allow users to edit Quantity field.
        break;
    }
  } 
  return FALSE; // Disallow users to edit Quantity field.
}

It seems it works, as I didn't see any other simple way of doing it.

kenorb’s picture

So the feature would be eventually to implement the action rule, to disable or enable the Quantity widget type on certain condition (e.g. certain product type).
It is something to be considered, or it's just the edge case?

MarcElbichon’s picture

This code should work too :

function yourmodule_form_alter(&$form, &$form_state, $form_id) {
    if (strpos($form_id, 'views_form_commerce_cart_form_') === 0 ) {
        $form["edit_quantity"][0]["#attributes"]["readonly"] = "readonly";
        $form["edit_quantity"][0]["#attributes"]["disabled"] = TRUE;
    }
}

When you said Disable, you mean visible but not modifiable ?
On checkout page, quantity is not modifiable.
I think i could create an action, but form must be passed by the event. I'm trying to search a solution for.

kenorb’s picture

StatusFileSize
new15.21 KB

Yes, disable to be not modifiable or just plain text instead of text widget.
Check the screenshot for example.

MarcElbichon’s picture

Need a precision.
In Stackoverflow, you said you have disabled quantity field by managing display for product display.
Does this mean quantity field is "hidden" in product view but when you showed cart, quantity field is present and modifiable ?

MarcElbichon’s picture

Status: Active » Closed (won't fix)

Sorry, but i see no way to do this because we must interact with views.
Another method may be to add the raw quantity field after the quantity widget in the view (in the same column) and just play with css (display:none) in your template file according to product type. You can also use view customfield.

kenorb’s picture

I'm using Webform for adding product to the cart, so I'm not using standard quantity field on product view. So the only page where I can edit the quantity is a view (checkout and summary cart).
I think I'll stay with custom overriding TPL for now. Let see if there more interest in that kind of feature.
Thank you for your help.

StefCarr’s picture

I found this article on heavy kettle and this method works for me.

faadz’s picture

Hi There

The code below works if we want to disable quantity widget on shopping cart page for product types. What I need to do is to disable quantity widget on the basis of Product ids.

Or

Disable quantity widget on shopping cart page if a boolean field is true on product page.

Any suggestions?

Thanks

/**
* Implements hook_form_alter().
*/
function custom_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_form_commerce_cart_form_default') {
global $user;
$product_types = array('pickup', 'delivery');
$cart = commerce_cart_order_load($user->uid);
foreach (entity_metadata_wrapper('commerce_order', $cart)->commerce_line_items as $delta => $line_item_wrapper) {
// check current line item product type against list of types to be disabled
if (in_array($line_item_wrapper->commerce_product->value()->type, $product_types)) {
foreach ($form['edit_quantity'] as $index => $line_item) {
if (is_numeric($index)) {
// compare the current shopping cart line item ID to the form line item ID
if ($line_item_wrapper->raw() == $form["edit_quantity"][$index]['#line_item_id']) {
$form['edit_quantity'][$index]['#attributes']['readonly'] = 'readonly';
$form['edit_quantity'][$index]['#attributes']['disabled'] = TRUE;
$form['edit_quantity'][$index]['#attributes']['title'] = t('This quantity cannot be changed.');
unset($form['edit_delete'][$index]);
}
}
}
}
}
}
}

faadz’s picture

Hi There

The code above by heavy kettle works if we want to disable quantity widget on shopping cart page for product types. What I need to do is to disable quantity widget on the basis of Product ids.

Or

Disable quantity widget on shopping cart page if a boolean field is true on product page.

Any suggestions?

Thanks

perignon’s picture

Yeah this is a feature hard to implement because it is a field being displayed in views and the field can change names.

faadz’s picture

Hi Perignon

I have managed to disable quantity widget on the basis of product ids using the code below. But this doesn't work for me as I would have to add new product ids to the code each time if quantity needs to be disabled for new products.

The way I am thinking is to add a custom boolean field to a product type and if the value of that field is true then the quantity widget should be disabled. Any idea how?


function MODULE_form_alter(&$form, $form_state, $form_id) {
		if ($form_id == 'views_form_commerce_cart_form_default') {
		global $user;
		$cart = commerce_cart_order_load($user->uid);
		foreach (commerce_order_load_multiple(array(), array('status' => 'pending'), TRUE) as $order) {
		$product_ids = array();
		foreach (entity_metadata_wrapper('commerce_order', $cart)->commerce_line_items as $delta => $line_item_wrapper) {
			if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
			   $product_ids[0] = $line_item_wrapper->commerce_product->raw();
			   
			}
			  		  if (in_array(221, $product_ids) || in_array(207, $product_ids)) {
					  foreach ($form['edit_quantity'] as $index => $line_item) {
					 if (is_numeric($index)) {
						 
			              // compare the current shopping cart line item ID to the form line item ID
						   
							if ($line_item_wrapper->raw() == $form["edit_quantity"][$index]['#line_item_id']) {
								$form['edit_quantity'][$index]['#attributes']['readonly'] = 'readonly';
								$form['edit_quantity'][$index]['#attributes']['disabled'] = TRUE;
								$form['edit_quantity'][$index]['#attributes']['title'] = t('This quantity cannot be changed.');
								//unset($form['edit_delete'][$index]);
							}
					  }
					}
				}
			}
		}
	}
}

Thanks

perignon’s picture

Could you edit your post so it is easier to read. Use the code or PHP formatting in the editor.

faadz’s picture

Hi Perignon

I have updated the post now.

Thanks
Faadz

destash’s picture

thanks all