Skip to content

Instantly share code, notes, and snippets.

@coyoterj
Last active December 21, 2015 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coyoterj/6247377 to your computer and use it in GitHub Desktop.
Save coyoterj/6247377 to your computer and use it in GitHub Desktop.
Include a field on the "quick edit" form to integrate Drupal Commerce Stock with Drupal Commerce Backoffice
/**
* alter function for commerce_backoffice_product_quick_edit_form.
*/
function YOURMODULE_form_commerce_backoffice_product_quick_edit_form_alter(&$form, &$form_state, &$product){
$product = $form_state['product'] ;
$form['stock'] = array(
'#type' => 'textfield',
'#title' => t('Stock'),
'#title_display' => 'invisible',
'#default_value' => $product->commerce_stock['und'][0]['value'],
'#size' => 5,
);
$form['#submit'][] = YOURMODULE_commerce_backoffice_product_quick_edit_form_submit';
}
/**
* Submit callback for commerce_backoffice_product_quick_edit_form.
*/
function YOURMODULE_commerce_backoffice_product_quick_edit_form_submit($form, &$form_state) {
$product = $form_state['product'];
$currency_code = $product->commerce_price[LANGUAGE_NONE][0]['currency_code'];
$product->commerce_price[LANGUAGE_NONE][0]['amount'] = commerce_currency_decimal_to_amount($form_state['values']['price'], $currency_code);
$product->status = $form_state['values']['status'];
$product->commerce_stock[LANGUAGE_NONE][0]['value'] = $form_state['values']['stock'];
commerce_product_save($product);
$form_state['rebuild'] = TRUE;
$form_state['product_saved'] = TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment