function commerce_stock_admin_form_submit in Commerce Stock 7
Add or remove the Stock field from product types.
File
- includes/
commerce_stock.admin.inc, line 121 - Administrative callbacks and form builder functions for Commerce Stock.
Code
function commerce_stock_admin_form_submit($form, &$form_state) {
$form_state['commerce_stock']['delete_instances'] = array();
foreach ($form_state['values']['product_types'] as $type => $enable) {
$instance = field_info_instance('commerce_product', 'commerce_stock', $type);
$currently_enabled = commerce_stock_product_type_enabled($type);
// If they want us to enable it and it doesn't currently exist, do the work.
if ($enable && !$currently_enabled) {
commerce_stock_create_instance('commerce_stock', 'number_integer', TRUE, 'commerce_product', $type, t('Stock'));
//Initialize fields of every existing product of $type bundle to 0
commerce_stock_admin_init($type, 0);
drupal_set_message(t('Stock management has been enabled on the %type product type', array(
'%type' => $type,
)));
}
else {
if (!$enable && $currently_enabled) {
// If they haven't clicked the "confirm" checkbox, rebuild and get them
// to do it.
if (empty($form_state['values']['confirmation'])) {
$form_state['commerce_stock']['delete_instances'][] = $type;
$form_state['rebuild'] = TRUE;
}
else {
// Remove the instance.
field_delete_instance($instance);
// Remove override if enabled
if (commerce_stock_product_type_override_enabled($type)) {
$override = field_info_instance('commerce_product', 'commerce_stock_override', $type);
field_delete_instance($override);
}
drupal_set_message(t('Stock management has been disabled on the %type product type', array(
'%type' => $type,
)));
}
}
}
}
if (!empty($form_state['values']['product_types_override'])) {
foreach ($form_state['values']['product_types_override'] as $type => $enable) {
$instance = field_info_instance('commerce_product', 'commerce_stock_override', $type);
$currently_enabled = commerce_stock_product_type_override_enabled($type);
$stock_enabled = commerce_stock_product_type_enabled($type);
// If they want us to enable it and it doesn't currently exist, do the work.
if ($enable && $stock_enabled && !$currently_enabled) {
commerce_stock_create_instance('commerce_stock_override', 'list_boolean', FALSE, 'commerce_product', $type, t('Disable stock for this product'));
drupal_set_message(t('Stock management override has been enabled on the %type product type', array(
'%type' => $type,
)));
}
else {
if (!$enable && $currently_enabled) {
// If they haven't clicked the "confirm" checkbox, rebuild and get them
// to do it.
if (empty($form_state['values']['confirmation'])) {
$form_state['commerce_stock']['delete_instances'][] = $type;
$form_state['rebuild'] = TRUE;
}
else {
// Remove the instance.
field_delete_instance($instance);
drupal_set_message(t('Stock management override has been disabled on the %type product type', array(
'%type' => $type,
)));
}
}
}
}
}
}