Guest User

Untitled

a guest
Jul 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. function bb_add_stock_reducer_single_product_field() {
  2. ?><div class='options_group'><?php
  3. woocommerce_wp_text_input( array(
  4. 'id' => '_stock_reducer',
  5. 'label' => __( 'Inventory reduction per item sold', 'woocommerce' ),
  6. 'placeholder' => __( 'When purchased, reduce stock level with this much', 'woocommerce' ),
  7. 'type' => 'number',
  8. 'custom_attributes' => array(
  9. 'min' => '1',
  10. 'step' => '1',
  11. ),
  12. ) );
  13. ?></div><?php
  14. }
  15. add_action( 'woocommerce_product_options_inventory_product_data', 'bb_add_stock_reducer_single_product_field' );
  16.  
  17.  
  18. function bb_add_stock_reducer_variable_product_field( $loop, $variation_data, $variation ) {
  19. $variation = wc_get_product( $variation );
  20. woocommerce_wp_text_input( array(
  21. 'id' => "stock_multiplier{$loop}",
  22. 'name' => "stock_multiplier[{$loop}]",
  23. 'value' => $variation->get_meta( '_stock_reducer' ),
  24. 'label' => __( 'Inventory reduction per variable item sold', 'woocommerce' ),
  25. 'placeholder' => __( 'When purchased, reduce stock level with this much', 'woocommerce' ),
  26. 'type' => 'number',
  27. 'custom_attributes' => array(
  28. 'min' => '1',
  29. 'step' => '1',
  30. ),
  31. ) );
  32. }
  33. add_action( 'woocommerce_variation_options_pricing', 'bb_add_stock_reducer_variable_product_field', 50, 3 );
  34.  
  35.  
  36. function bb_save_single_product_stock_reducer( $product ) {
  37. if ( ! empty( $_POST['_stock_reducer'] ) ) {
  38. $product->update_meta_data( '_stock_reducer', absint( $_POST['_stock_reducer'] ) );
  39. }
  40. }
  41. add_action( 'woocommerce_admin_process_product_object', 'bb_save_single_product_stock_reducer' );
  42.  
  43.  
  44. function bb_save_variable_product_stock_reducer( $variation_id, $i ) {
  45. $variation = wc_get_product( $variation_id );
  46. if ( ! empty( $_POST['stock_multiplier'] ) && ! empty( $_POST['stock_multiplier'][ $i ] ) ) {
  47. $variation->update_meta_data( '_stock_reducer', absint( $_POST['stock_multiplier'][ $i ] ) );
  48. $variation->save();
  49. }
  50. }
  51. add_action( 'woocommerce_save_product_variation', 'bb_save_variable_product_stock_reducer', 10, 2 );
  52.  
  53.  
  54. function bb_stock_reducer( $quantity, $order, $item ) {
  55. $multiplier = $item->get_product()->get_meta( '_stock_reducer' );
  56. if ( empty( $multiplier ) && $item->get_product()->is_type( 'variation' ) ) {
  57. $product = wc_get_product( $item->get_product()->get_parent_id() );
  58. $multiplier = $product->get_meta( '_stock_reducer' );
  59. }
  60. if ( ! empty( $multiplier ) ) {
  61. $quantity = $multiplier * $quantity;
  62. }
  63. return $quantity;
  64. }
  65. add_filter( 'woocommerce_order_item_quantity', 'bb_stock_reducer', 10, 3 );
Add Comment
Please, Sign In to add comment