function commerce_stock_form_alter in Commerce Stock 7
Same name and namespace in other branches
- 7.2 commerce_stock.module \commerce_stock_form_alter()
Implements hook_form_alter().
Alters the add-to-cart form to show out-of-stock items and add a validator.
File
- ./
commerce_stock.module, line 46 - Allow commerce products to have stock levels associated with their SKU
Code
function commerce_stock_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {
$stock = array();
$stock_enabled = array();
// check if product is disabled
if (isset($form['submit']['#attributes']['disabled']) && $form['submit']['#attributes']['disabled'] == 'disabled') {
return;
}
// Check to see if product has options (multiple products using
// the default dropdown)
if (isset($form['product_id']['#options'])) {
$options = $form['product_id']['#options'];
// Stock validation (for options):
// Stock validation only true if stock field exist
// @todo: for version 2 we need a better way to determine if
// stock enabled and not rely on the existence of the stock field
$stock_validation = FALSE;
foreach ($options as $key => $value) {
$product = commerce_product_load($key);
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
if (!empty($product_wrapper->commerce_stock)) {
$stock_validation = TRUE;
// Set stock array
$stock[$key] = $product_wrapper->commerce_stock
->value();
// set stock enabled array
if (isset($product_wrapper->commerce_stock_override) && $product_wrapper->commerce_stock_override
->value() == 1) {
$stock_enabled[$key] = FALSE;
}
else {
$stock_enabled[$key] = TRUE;
}
}
else {
// stock is disabled or the product has no value (we will handle it as 0)
// @todo if stock field had a default of 0 this wont be needed
$stock_enabled[$key] = TRUE;
$stock[$key] = 0;
}
}
// Stock validation actions (for options):
if ($stock_validation) {
// set "out of stock" Message
foreach ($options as $key => $value) {
if ($stock_enabled[$key] && $stock[$key] <= 0) {
$form['product_id']['#options'][$key] .= ' - ' . t('Out of Stock');
}
}
// Set validation
$form['#after_build'][] = 'commerce_stock_form_after_build';
$form['product_id']['#element_validate'][] = 'commerce_stock_option_validate';
$form['product_id']['#stock'] = $stock;
$form['product_id']['#stock_enabled'] = $stock_enabled;
}
}
elseif (isset($form['product_id']['#value'])) {
$product = commerce_product_load($form['product_id']['#value']);
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
// Add validation to the add to cart
$form['#validate'][] = 'commerce_stock_add_to_cart_validate';
if (isset($product_wrapper->commerce_stock)) {
if (!(isset($product_wrapper->commerce_stock_override) && $product_wrapper->commerce_stock_override
->value() == 1)) {
if ($product_wrapper->commerce_stock
->value() <= 0) {
$form['submit']['#value'] = t('Out of Stock');
$form['submit']['#disabled'] = TRUE;
$form['submit']['#attributes'] = array(
'class' => array(
'out-of-stock',
),
);
}
}
}
}
}
elseif ($form_id == 'commerce_checkout_form_checkout') {
// Add validate function to the checkout form
$form['buttons']['continue']['#validate'][] = 'commerce_stock_checkout_form_validate';
}
elseif ($form_id == 'commerce_checkout_form_review') {
// Add validate function to the review form
// /@todo: would be good to prompt the user with some contextual info
// as he was about to pay
$form['buttons']['continue']['#validate'][] = 'commerce_stock_checkout_form_validate';
}
}