Guest User

Untitled

a guest
Feb 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. class Comfythemes_Woocommerce_Auto_Stock_Restore {
  2.  
  3. function __construct() {
  4. add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  5. add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  6. add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  7. add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  8. add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  9. add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  10. }
  11. public function restore_order_stock( $order_id ) {
  12. $order = new WC_Order( $order_id );
  13.  
  14. if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
  15. return;
  16. }
  17. foreach ( $order->get_items() as $item ) {
  18. if ( $item['product_id'] > 0 ) {
  19.  
  20. $_product = $order->get_product_from_item( $item );
  21.  
  22. if ( $_product && $_product->exists() && $_product->managing_stock() ) {
  23.  
  24. $old_stock = $_product->stock;
  25. $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
  26. $new_quantity = $_product->increase_stock( $qty );
  27. do_action( 'woocommerce_auto_stock_restored', $_product, $item );
  28. $order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
  29. $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
  30. }
  31. }
  32. }
  33. }
  34. }
  35.  
  36. new Comfythemes_Woocommerce_Auto_Stock_Restore();
  37.  
  38. if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) {
  39. class WC_Auto_Stock_Restore {
  40.  
  41. function __construct() {
  42. add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
  43. add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
  44. add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
  45. add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
  46. add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
  47. add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
  48. }
  49.  
  50. public function restore_order_stock( $order_id, $order ) {
  51. $items = $order->get_items();
  52.  
  53. if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! count( $items ) > 0 )
  54. return; // We exit
  55.  
  56. foreach ( $order->get_items() as $item ) {
  57. $product_id = $item->get_product_id();
  58.  
  59. if ( $product_id > 0 ) {
  60. $product = $item->get_product();
  61.  
  62. if ( $product && $product->exists() && $product->managing_stock() ) {
  63.  
  64. // Get the product initial stock quantity (before update)
  65. $initial_stock = $product->get_stock_quantity();
  66.  
  67. $item_qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $this, $item );
  68.  
  69. // Update the product stock quantity
  70. // Replace DEPRECATED methods: increase_stock() & discrease_stock()
  71. wc_update_product_stock( $product, $item_qty, 'increase' );
  72.  
  73. // Get the product updated stock quantity
  74. $updated_stock = $initial_stock + $item_qty;
  75.  
  76. do_action( 'woocommerce_auto_stock_restored', $product, $item );
  77.  
  78. // A unique Order note: Store each order note in an array…
  79. $order_note[] = sprintf( __( 'Product ID #%s stock incremented from %s to %s.', 'woocommerce' ), $product_id, $initial_stock, $updated_stock);
  80.  
  81. // DEPRECATED & NO LONGER NEEDED - can be removed
  82. //$order->send_stock_notifications( $product, $updated_stock, $item_qty );
  83.  
  84. }
  85. }
  86. }
  87. // Adding a unique composite order note (for multiple items)
  88. $order_notes = count($order_note) > 1 ? implode(' | ', $order_note) : $order_note[0];
  89. $order->add_order_note( $order_notes );
  90. }
  91. }
  92. $GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore();
  93. }
  94.  
  95. class Comfythemes_Woocommerce_Auto_Stock_Restore {
  96.  
  97. function __construct() {
  98. add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  99. add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  100. add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
  101. add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  102. add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  103. add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
  104. }
  105. public function restore_order_stock( $order_id ) {
  106. $order = new WC_Order( $order_id );
  107.  
  108. if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
  109. return;
  110. }
  111. foreach ( $order->get_items() as $item ) {
  112. if ( $item['product_id'] > 0 ) {
  113.  
  114. $_product = $order->get_product_from_item( $item );
  115.  
  116. if ( $_product && $_product->exists() && $_product->managing_stock() ) {
  117.  
  118. $old_stock = $_product->stock;
  119. $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
  120. $new_quantity = $_product->increase_stock( $qty );
  121. do_action( 'woocommerce_auto_stock_restored', $_product, $item );
  122. $order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
  123. $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
  124. }
  125. }
  126. }
  127. }
  128. }
  129. new Comfythemes_Woocommerce_Auto_Stock_Restore()
Add Comment
Please, Sign In to add comment