It seems the Quantity widget is a matter of configuring the views.
If you'll edit one of the commerce views e.g. commerce_cart_form
or commerce_cart_summary
, Quantity can be found in the field list.
By default Commerce Cart module has Quantity set to text field (File: commerce_cart.views_default.inc), so to change the widget from text field to numeric and vice versa you need to edit your view and replace the Quantity field with other one.
There are two fields available for these views:
Commerce Line Item: Quantity
The quantity of the line item.
Machine name: quantity
Handler: views_handler_field_numeric
Commerce Line Item: Quantity text field
Adds a text field to edit the line item quantity in the View.)
Machine name: edit_quantity
Handler: commerce_line_item_handler_field_edit_quantity
If you need to switch between numeric and text widget on certain conditions, you'll have to alter the view by using hook_views_pre_view()
and change the handler on the fly.
Here is a simple example:
/**
* Implements hook_views_pre_view().
*/
function MODULENAME_views_pre_view(&$view) {
switch ($view->name) {
case 'commerce_cart_form':
case 'commerce_cart_summary':
$view->display['default']->handler->options['fields']['quantity']['field'] = 'edit_quantity'; // 'quantity' for numeric, 'edit_quantity' for text field
break;
}
}
Other method is to create TPL view template file i.e. views-view-field--commerce-cart-summary--default--quantity.tpl.php
and change the widget from there. Check 'Theme:Information' in view edit page for more info.
Here is example for restricting Quantity field based on the product type:
You need at least one TPL file created in your template directory (sites/all/themes/foo)
with the following PHP code:
print foo_quantity_available_for_row($row) ? $output : 1; // Hardcoded '1'.
Note: If Quantity is numeric, it'll always display 1, so extend it based on your needs.
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.
// $view->display['default']->handler->options['fields']['text_quantity'] = $view->display['default']->handler->options['fields']['quantity']; // Create clone.
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 could not work in all cases, so modify based on your needs.
Other method, as suggested by MarcElbichon would be to use hook_form_alter:
<?php
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;
}
}
?>
hook_form_alter()
. Beyond that the code largely depends on how you've implemented the quantity widget, and exactly what you want to do. We don't have a code writing service here so the thing to do is make some attempts, and if you have specific implementation problems ask about them hereform_alter()
I can't find any suitable Quantity element to change either in view it-self for Quantity. Or I'm missing something.hook_form_alter()
(that's true of any form element in Drupal; the Commerce modules most certainly use forms properly so anything they provide can be changed in ahook_form_alter()
). Implement ahook_form_alter()
, check the form ID, anddpm()
the form array to the screen. That'll show you where to find the required element(s)