Advertisement
sparkweb

Allow Manual Price Adjustment Based on Inventory

Sep 17th, 2011
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2. //Code to Allow Manual Price Adjustment Based on Inventory
  3. //Foxyshop 3.3+ required, place this in your functions.php file
  4.  
  5. //Manual Price Adjustment
  6. add_filter('foxyshop_setup_product_info', 'my_custom_price_setting', 10, 2);
  7. function my_custom_price_setting($product, $product_id) {
  8.     $pricing_levels = get_post_meta($product_id,'_pricing_levels',1);
  9.     if ($pricing_levels) {
  10.         $levels = explode("|", $pricing_levels);
  11.         $current_inventory = (int)$product['inventory_levels'][$product['code']]['count'];
  12.         $setprice = -1;
  13.         foreach($levels AS $level) {
  14.             $current_level = explode("-", $level);
  15.             if ($current_inventory <= (int)$current_level[0] && $setprice < 0) {
  16.                 $setprice = (double)$current_level[1];
  17.             }
  18.         }
  19.         if ($setprice >= 0) {
  20.             $product['price'] = $setprice;
  21.             $product['originalprice'] = $product['price'];
  22.         }
  23.     }
  24.     return $product;
  25. }
  26.  
  27. //Show New Field in Admin
  28. add_action('foxyshop_admin_product_details','my_custom_pricing_levels_meta');
  29. function my_custom_pricing_levels_meta($post_id) {
  30.     $_pricing_levels = get_post_meta($post_id,'_pricing_levels',1);
  31.     ?>
  32.     <div style="clear:both; padding-top: 6px;"></div>
  33.     <div class="foxyshop_field_control">
  34.         <label for="_pricing_levels"><?php _e('Price Levels'); ?></label>
  35.         <input type="text" name="_pricing_levels" id="_pricing_levels" value="<?php echo esc_attr($_pricing_levels); ?>" />
  36.     </div>
  37.     <div style="clear:both"></div>
  38.     <?php
  39. }
  40.  
  41. //Save New Field
  42. add_action('foxyshop_save_product','my_custom_pricing_levels_meta_save');
  43. function my_custom_pricing_levels_meta_save($post_id) {
  44.         global $post_id;
  45.         foxyshop_save_meta_data('_pricing_levels',$_POST['_pricing_levels']);
  46.         return $post_id;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement