Variable_product_stock_reduction

GQ class.

Defined (2)

The class is defined in the following location(s).

/includes/class-variable-product-stock-reduction.php  
  1. class Variable_product_stock_reduction{ 
  2. /** 
  3. * Plugin version, used for cache-busting of style and script file references. 
  4. * @since 1.0.0 
  5. * @var string 
  6. */ 
  7. protected $version = "1.0.0"; 
  8.  
  9. /** 
  10. * Unique identifier for your plugin. 
  11. * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should 
  12. * match the Text Domain file header in the main plugin file. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. protected $plugin_slug = "gq"; 
  17.  
  18. /** 
  19. * Instance of this class. 
  20. * @since 1.0.0 
  21. * @var object 
  22. */ 
  23. protected static $instance = null; 
  24.  
  25. /** 
  26. * Slug of the plugin screen. 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. protected $plugin_screen_hook_suffix = null; 
  31.  
  32. /** 
  33. * Initialize the plugin by setting localization, filters, and administration functions. 
  34. * @since 1.0.0 
  35. */ 
  36. private function __construct() { 
  37.  
  38. //Display Fields 
  39. add_action( 'woocommerce_product_after_variable_attributes', array($this, 'variable_fields' ), 10, 3 ); 
  40. //JS to add fields for new variations 
  41. add_action( 'woocommerce_product_after_variable_attributes_js', array($this, 'variable_fields_js') ); 
  42. //Save variation fields 
  43. add_action( 'woocommerce_process_product_meta_variable', array($this, 'save_variable_fields'), 10, 1 ); 
  44. // 
  45. add_action( "woocommerce_checkout_order_processed", array($this, 'update_stock_on_checkout_nopayment') ); 
  46. //add_action( "woocommerce_payment_complete", array($this, 'update_stock_on_checkout') ); 
  47. //Stop woocommerce from reducing stock amounts 
  48. //add_filter( 'woocommerce_payment_complete_reduce_order_stock', '__return_false' ); 
  49. /** 
  50. * Return an instance of this class. 
  51. * @since 1.0.0 
  52. * @return object A single instance of this class. 
  53. */ 
  54. public static function get_instance() { 
  55.  
  56. // If the single instance hasn"t been set, set it now. 
  57. if (null == self::$instance) { 
  58. self::$instance = new self; 
  59.  
  60. return self::$instance; 
  61. /** 
  62. * Create new fields for variations 
  63. */ 
  64. public function variable_fields( $loop, $variation_data, $varPost ) { 
  65. $post_id = $varPost->ID; 
  66. $meta = get_post_meta( $post_id ); 
  67. ?> 
  68. <tr> 
  69. <td> 
  70. <?php 
  71. // Select 
  72. woocommerce_wp_select( 
  73. array( 
  74. 'id' => '_deductornot['.$loop.']',  
  75. 'label' => __( 'Deduct from stock total', 'woocommerce' ),  
  76. 'description' => __( 'Choose a value.', 'woocommerce' ),  
  77. 'value' => $meta['_deductornot'][0],  
  78. 'options' => array( 
  79. 'no' => __( 'No', 'woocommerce' ),  
  80. 'yes' => __( 'Yes', 'woocommerce' ),  
  81. ); 
  82. ?> 
  83. </td> 
  84. </tr> 
  85. <tr> 
  86. <td> 
  87. <?php 
  88. // Number Field 
  89. woocommerce_wp_text_input( 
  90. array( 
  91. 'id' => '_deductamount['.$loop.']',  
  92. 'label' => __( 'Amount to deduct from total', 'woocommerce' ),  
  93. 'desc_tip' => 'true',  
  94. 'description' => __( 'The amount of stock to deduct from the product total stock upon purchase.', 'woocommerce' ),  
  95. 'value' => $meta['_deductamount'][0],  
  96. 'custom_attributes' => array( 
  97. 'step' => 'any',  
  98. 'min' => '0' 
  99. ); 
  100. ?> 
  101. </td> 
  102. </tr> 
  103. <?php 
  104.  
  105. /** 
  106. * Create new fields for new variations 
  107. */ 
  108. public function variable_fields_js() { 
  109. ?> 
  110. <tr> 
  111. <td> 
  112. <?php 
  113. // Select 
  114. woocommerce_wp_select( 
  115. array( 
  116. 'id' => '_deductornot[ + loop + ]',  
  117. 'label' => __( 'Deduct from stock total', 'woocommerce' ),  
  118. 'description' => __( 'Choose a value.', 'woocommerce' ),  
  119. 'value' => $variation_data['_deductornot'][0],  
  120. 'options' => array( 
  121. 'no' => __( 'No', 'woocommerce' ),  
  122. 'yes' => __( 'Yes', 'woocommerce' ),  
  123. ); 
  124. ?> 
  125. </td> 
  126. </tr> 
  127. <tr> 
  128. <td> 
  129. <?php 
  130. // Number Field 
  131. woocommerce_wp_text_input( 
  132. array( 
  133. 'id' => '_deductamount[ + loop + ]',  
  134. 'label' => __( 'Amount to deduct from total', 'woocommerce' ),  
  135. 'desc_tip' => 'true',  
  136. 'description' => __( 'The amount of stock to deduct from the product total stock upon purchase.', 'woocommerce' ),  
  137. 'value' => $variation_data['_deductamount'][0],  
  138. 'custom_attributes' => array( 
  139. 'step' => 'any',  
  140. 'min' => '0' 
  141. ); 
  142. ?> 
  143. </td> 
  144. </tr> 
  145. <?php 
  146.  
  147. /** 
  148. * Save new fields for variations 
  149. */ 
  150. public function save_variable_fields( $post_id ) { 
  151. if (isset( $_POST['variable_sku'] ) ) : 
  152.  
  153. $variable_sku = $_POST['variable_sku']; 
  154. $variable_post_id = $_POST['variable_post_id']; 
  155.  
  156.  
  157. // Number Field 
  158. $_deductamount = $_POST['_deductamount']; 
  159. for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : 
  160. $variation_id = (int) $variable_post_id[$i]; 
  161. if ( isset( $_deductamount[$i] ) ) { 
  162. update_post_meta( $variation_id, '_deductamount', stripslashes( $_deductamount[$i] ) ); 
  163. endfor; 
  164.  
  165.  
  166. // Select 
  167. $_deductornot = $_POST['_deductornot']; 
  168. for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : 
  169. $variation_id = (int) $variable_post_id[$i]; 
  170. if ( isset( $_deductornot[$i] ) ) { 
  171. update_post_meta( $variation_id, '_deductornot', stripslashes( $_deductornot[$i] ) ); 
  172. endfor; 
  173.  
  174. endif; 
  175. /** 
  176. */ 
  177. public function update_stock_on_checkout($order_id) { 
  178. global $woocommerce; 
  179.  
  180. // order object (optional but handy) 
  181.  
  182. $order = new WC_Order( $order_id ); 
  183. $items = $order->get_items(); 
  184. $varation_ids = array(); 
  185. $i = 0; 
  186.  
  187. foreach ($items as $key => $value) { 
  188. # code... 
  189. $variation_ids[$i]['variation_id'] = $value['variation_id']; 
  190. $variation_ids[$i]['product_id'] = $value['product_id']; 
  191. $variation_ids[$i]['qty'] = $value['qty']; 
  192. $i++; 
  193. foreach($variation_ids as $key => $value) { 
  194.  
  195. $deductornot = get_post_meta( $value["variation_id"], '_deductornot', true );; 
  196. $deductamount = get_post_meta( $value["variation_id"], '_deductamount', true );; 
  197. $product_id = $value['product_id']; 
  198. $qty = $value['qty']; 
  199. $product = wc_get_product( $product_id ); 
  200.  
  201. if($deductornot == "yes") { 
  202. $currentstock = $product->get_stock_quantity(); 
  203. $reduceamount = intval($qty) * intval($deductamount - $qty); 
  204. $newstock = intval($currentstock) - $reduceamount; 
  205. $updatestock = $product->set_stock( $newstock );; 
  206.  
  207. }else{ 
  208. //$product->reduce_stock( $qty ); 
  209.  
  210.  
  211. //return false; 
  212. //die(); 
  213. /** 
  214. */ 
  215. public function update_stock_on_checkout_nopayment($order_id) { 
  216. global $woocommerce, $wpdb; 
  217.  
  218. // order object (optional but handy) 
  219.  
  220. $order = new WC_Order( $order_id ); 
  221. $items = $order->get_items(); 
  222. $varation_ids = array(); 
  223. $i = 0; 
  224.  
  225. foreach ($items as $key => $value) { 
  226. # code... 
  227. $variation_ids[$i]['variation_id'] = $value['variation_id']; 
  228. $variation_ids[$i]['product_id'] = $value['product_id']; 
  229. $variation_ids[$i]['qty'] = $value['qty']; 
  230. $i++; 
  231. foreach($variation_ids as $key => $value) { 
  232.  
  233. $deductornot = get_post_meta( $value["variation_id"], '_deductornot', true );; 
  234. $deductamount = get_post_meta( $value["variation_id"], '_deductamount', true );; 
  235. $product_id = $value['product_id']; 
  236. $qty = $value['qty']; 
  237. $product = wc_get_product( $product_id ); 
  238.  
  239. if($deductornot == "yes") { 
  240. $currentstock = $product->get_stock_quantity(); 
  241. $reduceamount = ( intval($qty) * intval($deductamount) ) - intval($qty); 
  242. $newstock = intval($currentstock) - $reduceamount; 
  243. $updatestock = $product->set_stock( $newstock );; 
  244.  
  245. }else{ 
  246.  
  247.  
  248. //$product->reduce_stock( $qty ); 
  249. // Clear caches 
  250. /** 
  251. return false; 
  252. die(); 
  253. */ 
  254. // delete all "namespace" transients 
  255. $sql = "DELETE FROM {$wpdb->options} WHERE option_name like '\_transient\_timeout\_wc\_%' "; 
  256.  
  257. $wpdb->query($sql); 
  258. /** 
  259.   
  260. var_dump($del); 
  261. */ 
  262. //return false; 
  263. //die(); 
/trunk/includes/class-variable-product-stock-reduction.php  
  1. class Variable_product_stock_reduction{ 
  2. /** 
  3. * Plugin version, used for cache-busting of style and script file references. 
  4. * @since 1.0.0 
  5. * @var string 
  6. */ 
  7. protected $version = "1.0.0"; 
  8.  
  9. /** 
  10. * Unique identifier for your plugin. 
  11. * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should 
  12. * match the Text Domain file header in the main plugin file. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. protected $plugin_slug = "gq"; 
  17.  
  18. /** 
  19. * Instance of this class. 
  20. * @since 1.0.0 
  21. * @var object 
  22. */ 
  23. protected static $instance = null; 
  24.  
  25. /** 
  26. * Slug of the plugin screen. 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. protected $plugin_screen_hook_suffix = null; 
  31.  
  32. /** 
  33. * Initialize the plugin by setting localization, filters, and administration functions. 
  34. * @since 1.0.0 
  35. */ 
  36. private function __construct() { 
  37.  
  38. //Display Fields 
  39. add_action( 'woocommerce_product_after_variable_attributes', array($this, 'variable_fields' ), 10, 3 ); 
  40. //JS to add fields for new variations 
  41. add_action( 'woocommerce_product_after_variable_attributes_js', array($this, 'variable_fields_js') ); 
  42. //Save variation fields 
  43. add_action( 'woocommerce_process_product_meta_variable', array($this, 'save_variable_fields'), 10, 1 ); 
  44. // 
  45. add_action( "woocommerce_checkout_order_processed", array($this, 'update_stock_on_checkout_nopayment') ); 
  46. //add_action( "woocommerce_payment_complete", array($this, 'update_stock_on_checkout') ); 
  47. //Stop woocommerce from reducing stock amounts 
  48. //add_filter( 'woocommerce_payment_complete_reduce_order_stock', '__return_false' ); 
  49. /** 
  50. * Return an instance of this class. 
  51. * @since 1.0.0 
  52. * @return object A single instance of this class. 
  53. */ 
  54. public static function get_instance() { 
  55.  
  56. // If the single instance hasn"t been set, set it now. 
  57. if (null == self::$instance) { 
  58. self::$instance = new self; 
  59.  
  60. return self::$instance; 
  61. /** 
  62. * Create new fields for variations 
  63. */ 
  64. public function variable_fields( $loop, $variation_data, $varPost ) { 
  65. $post_id = $varPost->ID; 
  66. $meta = get_post_meta( $post_id ); 
  67. ?> 
  68. <tr> 
  69. <td> 
  70. <?php 
  71. // Select 
  72. woocommerce_wp_select( 
  73. array( 
  74. 'id' => '_deductornot['.$loop.']',  
  75. 'label' => __( 'Deduct from stock total', 'woocommerce' ),  
  76. 'description' => __( 'Choose a value.', 'woocommerce' ),  
  77. 'value' => $meta['_deductornot'][0],  
  78. 'options' => array( 
  79. 'no' => __( 'No', 'woocommerce' ),  
  80. 'yes' => __( 'Yes', 'woocommerce' ),  
  81. ); 
  82. ?> 
  83. </td> 
  84. </tr> 
  85. <tr> 
  86. <td> 
  87. <?php 
  88. // Number Field 
  89. woocommerce_wp_text_input( 
  90. array( 
  91. 'id' => '_deductamount['.$loop.']',  
  92. 'label' => __( 'Amount to deduct from total', 'woocommerce' ),  
  93. 'desc_tip' => 'true',  
  94. 'description' => __( 'The amount of stock to deduct from the product total stock upon purchase.', 'woocommerce' ),  
  95. 'value' => $meta['_deductamount'][0],  
  96. 'custom_attributes' => array( 
  97. 'step' => 'any',  
  98. 'min' => '0' 
  99. ); 
  100. ?> 
  101. </td> 
  102. </tr> 
  103. <?php 
  104.  
  105. /** 
  106. * Create new fields for new variations 
  107. */ 
  108. public function variable_fields_js() { 
  109. ?> 
  110. <tr> 
  111. <td> 
  112. <?php 
  113. // Select 
  114. woocommerce_wp_select( 
  115. array( 
  116. 'id' => '_deductornot[ + loop + ]',  
  117. 'label' => __( 'Deduct from stock total', 'woocommerce' ),  
  118. 'description' => __( 'Choose a value.', 'woocommerce' ),  
  119. 'value' => $variation_data['_deductornot'][0],  
  120. 'options' => array( 
  121. 'no' => __( 'No', 'woocommerce' ),  
  122. 'yes' => __( 'Yes', 'woocommerce' ),  
  123. ); 
  124. ?> 
  125. </td> 
  126. </tr> 
  127. <tr> 
  128. <td> 
  129. <?php 
  130. // Number Field 
  131. woocommerce_wp_text_input( 
  132. array( 
  133. 'id' => '_deductamount[ + loop + ]',  
  134. 'label' => __( 'Amount to deduct from total', 'woocommerce' ),  
  135. 'desc_tip' => 'true',  
  136. 'description' => __( 'The amount of stock to deduct from the product total stock upon purchase.', 'woocommerce' ),  
  137. 'value' => $variation_data['_deductamount'][0],  
  138. 'custom_attributes' => array( 
  139. 'step' => 'any',  
  140. 'min' => '0' 
  141. ); 
  142. ?> 
  143. </td> 
  144. </tr> 
  145. <?php 
  146.  
  147. /** 
  148. * Save new fields for variations 
  149. */ 
  150. public function save_variable_fields( $post_id ) { 
  151. if (isset( $_POST['variable_sku'] ) ) : 
  152.  
  153. $variable_sku = $_POST['variable_sku']; 
  154. $variable_post_id = $_POST['variable_post_id']; 
  155.  
  156.  
  157. // Number Field 
  158. $_deductamount = $_POST['_deductamount']; 
  159. for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : 
  160. $variation_id = (int) $variable_post_id[$i]; 
  161. if ( isset( $_deductamount[$i] ) ) { 
  162. update_post_meta( $variation_id, '_deductamount', stripslashes( $_deductamount[$i] ) ); 
  163. endfor; 
  164.  
  165.  
  166. // Select 
  167. $_deductornot = $_POST['_deductornot']; 
  168. for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : 
  169. $variation_id = (int) $variable_post_id[$i]; 
  170. if ( isset( $_deductornot[$i] ) ) { 
  171. update_post_meta( $variation_id, '_deductornot', stripslashes( $_deductornot[$i] ) ); 
  172. endfor; 
  173.  
  174. endif; 
  175. /** 
  176. */ 
  177. public function update_stock_on_checkout($order_id) { 
  178. global $woocommerce; 
  179.  
  180. // order object (optional but handy) 
  181.  
  182. $order = new WC_Order( $order_id ); 
  183. $items = $order->get_items(); 
  184. $varation_ids = array(); 
  185. $i = 0; 
  186.  
  187. foreach ($items as $key => $value) { 
  188. # code... 
  189. $variation_ids[$i]['variation_id'] = $value['variation_id']; 
  190. $variation_ids[$i]['product_id'] = $value['product_id']; 
  191. $variation_ids[$i]['qty'] = $value['qty']; 
  192. $i++; 
  193. foreach($variation_ids as $key => $value) { 
  194.  
  195. $deductornot = get_post_meta( $value["variation_id"], '_deductornot', true );; 
  196. $deductamount = get_post_meta( $value["variation_id"], '_deductamount', true );; 
  197. $product_id = $value['product_id']; 
  198. $qty = $value['qty']; 
  199. $product = wc_get_product( $product_id ); 
  200.  
  201. if($deductornot == "yes") { 
  202. $currentstock = $product->get_stock_quantity(); 
  203. $reduceamount = intval($qty) * intval($deductamount - $qty); 
  204. $newstock = intval($currentstock) - $reduceamount; 
  205. $updatestock = $product->set_stock( $newstock );; 
  206.  
  207. }else{ 
  208. //$product->reduce_stock( $qty ); 
  209.  
  210.  
  211. //return false; 
  212. //die(); 
  213. /** 
  214. */ 
  215. public function update_stock_on_checkout_nopayment($order_id) { 
  216. global $woocommerce, $wpdb; 
  217.  
  218. // order object (optional but handy) 
  219.  
  220. $order = new WC_Order( $order_id ); 
  221. $items = $order->get_items(); 
  222. $varation_ids = array(); 
  223. $i = 0; 
  224.  
  225. foreach ($items as $key => $value) { 
  226. # code... 
  227. $variation_ids[$i]['variation_id'] = $value['variation_id']; 
  228. $variation_ids[$i]['product_id'] = $value['product_id']; 
  229. $variation_ids[$i]['qty'] = $value['qty']; 
  230. $i++; 
  231. foreach($variation_ids as $key => $value) { 
  232.  
  233. $deductornot = get_post_meta( $value["variation_id"], '_deductornot', true );; 
  234. $deductamount = get_post_meta( $value["variation_id"], '_deductamount', true );; 
  235. $product_id = $value['product_id']; 
  236. $qty = $value['qty']; 
  237. $product = wc_get_product( $product_id ); 
  238.  
  239. if($deductornot == "yes") { 
  240. $currentstock = $product->get_stock_quantity(); 
  241. $reduceamount = ( intval($qty) * intval($deductamount) ) - intval($qty); 
  242. $newstock = intval($currentstock) - $reduceamount; 
  243. $updatestock = $product->set_stock( $newstock );; 
  244.  
  245. }else{ 
  246.  
  247.  
  248. //$product->reduce_stock( $qty ); 
  249. // Clear caches 
  250. /** 
  251. return false; 
  252. die(); 
  253. */ 
  254. // delete all "namespace" transients 
  255. $sql = "DELETE FROM {$wpdb->options} WHERE option_name like '\_transient\_timeout\_wc\_%' "; 
  256.  
  257. $wpdb->query($sql); 
  258. /** 
  259.   
  260. var_dump($del); 
  261. */ 
  262. //return false; 
  263. //die();