Advertisement
sparkweb

FoxyShop: Remove Out of Inventory Options

Jun 11th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. //Remove Out of Inventory Options From Product Variations
  2. //This should go in your functions.php file
  3. add_filter('foxyshop_setup_product_info', 'my_product_setup', 10, 2);
  4. function my_product_setup($product_array, $product_id) {
  5.  
  6.     //Remove Variation Options with inventory count of 0
  7.     foreach ($product_array['variations'] as $key => $variation) {
  8.  
  9.         $variation_list = preg_split("[\r\n|\r|\n]", $variation['value']);
  10.         if (count($variation_list) > 0) {
  11.             foreach ($variation_list as $var_key => $var_val) {
  12.                 $code = get_variation_code($var_val);
  13.                 if (!$code) continue;
  14.                 if (!check_product_code_inventory($code, $product_array['inventory_levels'])) {
  15.                     unset($variation_list[$var_key]);
  16.                 }
  17.             }
  18.             $variation['value'] = implode("\n", $variation_list);
  19.             $product_array['variations'][$key] = $variation;
  20.         }
  21.     }
  22.  
  23.     //Always return the new, complete array
  24.     return $product_array;
  25.  
  26. }
  27.  
  28. function check_product_code_inventory($code, $inventory_array) {
  29.     if (isset($inventory_array[$code])) {
  30.         if ($inventory_array[$code]['count'] <= 0) {
  31.             return false;
  32.         }
  33.     }
  34.     return true;
  35. }
  36.  
  37. function get_variation_code($source) {
  38.     if (strpos($source,"{") === false) return "";
  39.  
  40.     //Isolate the Modifiers
  41.     $variation_modifiers = substr($source, strpos($source,"{")+1, strpos($source,"}") - (strpos($source,"{")+1));
  42.     $arr_variation_modifiers = explode("|",$variation_modifiers);
  43.  
  44.     //Loop Through Each Modifier
  45.     foreach ($arr_variation_modifiers as $individual_modifier) {
  46.         if (strtolower(substr($individual_modifier,0,2)) == "c:") {
  47.             return substr($individual_modifier,2);
  48.         }
  49.     }
  50.  
  51.     //None Found
  52.     return "";
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement