/trunk/includes/class-variable-product-stock-reduction.php

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