-
Notifications
You must be signed in to change notification settings - Fork 5
/
commerce_simple_stock.module
62 lines (51 loc) · 1.64 KB
/
commerce_simple_stock.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
use Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function commerce_simple_stock_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the commerce_simple_stock module.
case 'help.page.commerce_simple_stock':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Allows commerce_simple_stock.') . '</p>';
// Here should be details about setting up and using
return $output;
default:
}
}
function commerce_simple_stock_theme($existing, $type, $theme, $path) {
return [
'commerce_simple_stock_inventory_control_form' => [
'render element' => 'form',
],
];
}
function commerce_simple_stock_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_data = $form_state->getStorage();
if (empty($form_data['product'])) {
return;
}
$selectedVariationId = $form_state->get('selected_variation');
if ($selectedVariationId != null) {
$variation = \Drupal\commerce_product\Entity\ProductVariation::load($selectedVariationId);
} else {
$product = $form_data['product'];
$variation = $product->getDefaultVariation();
}
if ($variation->hasField('field_stock') && $variation->field_stock->value != null) {
$stock = (integer) $variation->field_stock->value;
if ($stock <= 0) {
$form['quantity'] = null;
$form['actions'] = null;
$form['outofstock'] = [
'#type' => 'textfield',
'#value' => t('Out of Stock'),
'#weight' => 0,
'#disabled' => true,
];
}
}
}