| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* prevent purchasing pruducts below stock levels. |
|
* prevent purchasing of pruducts below stock levels. |
|
| 6 |
* |
* |
| 7 |
* this module includes some code from the Inventory API & Simple Stock Levels |
* this module includes some code from the Inventory API & Simple Stock Levels |
| 8 |
* (http://www.ubercart.org/contrib/132), by CpILL (http://www.ubercart.org/user/29). |
* (http://www.ubercart.org/contrib/132), by CpILL (http://www.ubercart.org/user/29). |
| 14 |
******************************************************************************/ |
******************************************************************************/ |
| 15 |
|
|
| 16 |
/** |
/** |
| 17 |
|
* Implementation of hook_theme(). |
| 18 |
|
*/ |
| 19 |
|
function uc_multi_stock_theme() { |
| 20 |
|
return array( |
| 21 |
|
'uc_multi_stock_out_of_stock_error' => array( |
| 22 |
|
'arguments' => array('stock' => NULL, 'threshold' => NULL, 'product' => NULL, 'attributes' => NULL), |
| 23 |
|
), |
| 24 |
|
'uc_multi_stock_too_many_in_order_error' => array( |
| 25 |
|
'arguments' => array('qty' => NULL, 'product' => NULL, 'attributes' => NULL), |
| 26 |
|
), |
| 27 |
|
); |
| 28 |
|
} |
| 29 |
|
|
| 30 |
|
/** |
| 31 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 32 |
*/ |
*/ |
| 33 |
function uc_multi_stock_menu($may_cache) { |
function uc_multi_stock_menu() { |
| 34 |
if ($may_cache) { |
$items['admin/store/settings/multi_stock'] = array( |
| 35 |
$items[] = array( |
'title' => 'Multi Stock Settings', |
| 36 |
'path' => 'admin/store/settings/multi_stock', |
'access callback' => 'user_access', |
| 37 |
'title' => t('Multi Stock Settings'), |
'access arguments' => array('administer store'), |
| 38 |
'access' => user_access('administer store'), |
'description' => 'Configure multi stock settings.', |
| 39 |
'description' => t('Configure multi stock settings.'), |
'page callback' => 'drupal_get_form', |
| 40 |
'callback' => 'drupal_get_form', |
'page arguments' => array('uc_multi_stock_settings'), |
| 41 |
'callback arguments' => array('uc_multi_stock_settings'), |
'type' => MENU_NORMAL_ITEM, |
| 42 |
'type' => MENU_NORMAL_ITEM, |
); |
|
); |
|
|
} |
|
| 43 |
return $items; |
return $items; |
| 44 |
} |
} |
| 45 |
|
|
| 48 |
* |
* |
| 49 |
* Add validation for cart view form. |
* Add validation for cart view form. |
| 50 |
*/ |
*/ |
| 51 |
function uc_multi_stock_form_alter($form_id, &$form) { |
function uc_multi_stock_form_alter(&$form, $form_state, $form_id) { |
| 52 |
// create validations for all the product forms |
// create validations for all the product forms |
| 53 |
switch ($form_id) { |
switch ($form_id) { |
| 54 |
case 'uc_cart_view_form': |
case 'uc_cart_view_form': |
| 55 |
//$form['#validate'] = array('uc_multi_stock_uc_cart_view_validate' => array()) + $form['#validate']; |
//$form['#validate'] = array('uc_multi_stock_uc_cart_view_validate' => array()) + $form['#validate']; |
| 56 |
$form['#validate'] = array('uc_multi_stock_uc_cart_view_validate' => array()); |
$form['#validate'][] = 'uc_multi_stock_uc_cart_view_validate'; |
| 57 |
break; |
break; |
| 58 |
} |
} |
| 59 |
} |
} |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
/** |
/** |
| 80 |
* Implementation of hook_add_to_cart(). |
* Implementation of hook_add_to_cart() |
| 81 |
* |
* |
| 82 |
* prevent addition of out of stock products |
* prevent addition of out of stock products |
| 83 |
*/ |
*/ |
| 115 |
* |
* |
| 116 |
* Final check with the Inventory Manager before allowing purchase |
* Final check with the Inventory Manager before allowing purchase |
| 117 |
*/ |
*/ |
| 118 |
function uc_multi_stock_order($op, &$order, $arg = NULL) { |
function uc_multi_stock_order($op, &$order, $arg = null) { |
| 119 |
switch ($op) { |
switch ($op) { |
| 120 |
case 'submit': |
case 'submit': |
| 121 |
$errors = array(); |
$errors = array(); |
| 164 |
/** |
/** |
| 165 |
* validate the view cart form |
* validate the view cart form |
| 166 |
*/ |
*/ |
| 167 |
function uc_multi_stock_uc_cart_view_validate($form_id, $form_values) { |
function uc_multi_stock_uc_cart_view_validate($form, &$form_state) { |
|
|
|
| 168 |
// for every item in the cart |
// for every item in the cart |
| 169 |
foreach ($form_values['items'] as $item) { |
foreach ($form_state['values']['items'] as $item) { |
| 170 |
$nid = $item['nid']; |
$nid = $item['nid']; |
| 171 |
$data = unserialize($item['data']); |
$data = unserialize($item['data']); |
| 172 |
|
|
| 176 |
$sku = uc_multi_get_sku($nid, $data['attributes']); |
$sku = uc_multi_get_sku($nid, $data['attributes']); |
| 177 |
|
|
| 178 |
// dont test the removal of out of stock products |
// dont test the removal of out of stock products |
| 179 |
if (!$item['remove'] && $item['qty']>0) { |
if (empty($item['remove']) && $item['qty']>0) { |
| 180 |
$tmp_error = uc_multi_stock_can_buy($product, $sku, $item['qty'], $data['attributes']); |
$tmp_error = uc_multi_stock_can_buy($product, $sku, $item['qty'], $data['attributes']); |
| 181 |
if ($tmp_error) { |
if ($tmp_error) { |
| 182 |
$error .= $tmp_error .'<br /><br />'; |
$error .= $tmp_error .'<br /><br />'; |
| 193 |
* Test if we can buy this product. |
* Test if we can buy this product. |
| 194 |
*/ |
*/ |
| 195 |
function uc_multi_stock_can_buy($product, $sku, $qty, $attributes) { |
function uc_multi_stock_can_buy($product, $sku, $qty, $attributes) { |
| 196 |
|
|
| 197 |
// test for missing sku - can happen when the deafult option is selected |
// test for missing sku - can happen when the deafult option is selected |
| 198 |
if (!$sku) { |
if (!$sku) { |
| 199 |
$sku = $product->model; |
$sku = $product->model; |
| 207 |
if (variable_get('uc_multi_stock_use_thershold', FALSE)) { |
if (variable_get('uc_multi_stock_use_thershold', FALSE)) { |
| 208 |
$stock_qty = $stock->threshold; |
$stock_qty = $stock->threshold; |
| 209 |
} |
} |
| 210 |
|
|
| 211 |
// check for out of stock |
// check for out of stock |
| 212 |
$error = uc_multi_stock_not_in_stock($stock->stock, $stock_qty, $product, $attributes); |
$error = uc_multi_stock_not_in_stock($stock->stock, $stock_qty, $product, $attributes); |
| 213 |
if ($error) { |
if ($error) { |
| 245 |
/** |
/** |
| 246 |
* themeable error messages |
* themeable error messages |
| 247 |
*/ |
*/ |
| 248 |
function theme_uc_multi_stock_out_of_stock_error($product = NULL, $attributes) { |
function theme_uc_multi_stock_out_of_stock_error($product = null, $attributes) { |
| 249 |
if (is_array($attributes) and count($attributes) > 0) { |
if (is_array($attributes) and count($attributes) > 0) { |
| 250 |
return (t("We're sorry. The @product in your selected size/color combination is out of stock. Please consider another size/color in this style.", array('@product' => $product->title))); |
return (t("We're sorry. The @product in your selected size/color combination is out of stock. Please consider another size/color in this style.", array('@product' => $product->title))); |
| 251 |
} |
} |
| 254 |
} |
} |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
function theme_uc_multi_stock_too_many_in_order_error($qty = 0, $product = NULL , $attributes) { |
function theme_uc_multi_stock_too_many_in_order_error($qty = 0, $product = null , $attributes) { |
| 258 |
if (is_array($attributes) and count($attributes) > 0) { |
if (is_array($attributes) and count($attributes) > 0) { |
| 259 |
return (t("We're sorry. We have only @qty unit(s) of the @product in your selected size/color combination left in stock. Please try again with fewer units or consider another size/color in this style.", array('@qty' => $qty, '@product' => $product->title))); |
return (t("We're sorry. We have only @qty unit(s) of the @product in your selected size/color combination left in stock. Please try again with fewer units or consider another size/color in this style.", array('@qty' => $qty, '@product' => $product->title))); |
| 260 |
} |
} |
| 303 |
$out = array(); |
$out = array(); |
| 304 |
|
|
| 305 |
if (!empty($nid) and module_exists('uc_attribute')) { |
if (!empty($nid) and module_exists('uc_attribute')) { |
| 306 |
$sql = 'SELECT * FROM {uc_product_adjustments} WHERE nid = '. $nid; |
$sql = 'SELECT * FROM {uc_product_adjustments} WHERE nid = '.$nid; |
| 307 |
$q = db_query($sql); |
$q = db_query($sql); |
| 308 |
$i = 0; |
$i = 0; |
| 309 |
while ($product = db_fetch_array($q)) { |
while ($product = db_fetch_array($q)) { |
| 310 |
$out[$i]['attributes'] = unserialize($product['combination']); |
$out[$i]['attributes'] = unserialize($product['combination']); |
| 311 |
$out[$i]['model'] = $product['model']; |
$out[$i]['model'] = $product['model']; |
| 312 |
$i++; |
$i++; |
| 313 |
} |
} |
| 314 |
return $out; |
return $out; |
| 315 |
} |
} |
| 316 |
else { |
else { |
| 317 |
return array(); |
return array(); |
| 318 |
} |
} |
| 319 |
} |
} |
| 320 |
|
|