Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bb_add_stock_reducer_single_product_field() {
- ?><div class='options_group'><?php
- woocommerce_wp_text_input( array(
- 'id' => '_stock_reducer',
- 'label' => __( 'Inventory reduction per item sold', 'woocommerce' ),
- 'placeholder' => __( 'When purchased, reduce stock level with this much', 'woocommerce' ),
- 'type' => 'number',
- 'custom_attributes' => array(
- 'min' => '1',
- 'step' => '1',
- ),
- ) );
- ?></div><?php
- }
- add_action( 'woocommerce_product_options_inventory_product_data', 'bb_add_stock_reducer_single_product_field' );
- function bb_add_stock_reducer_variable_product_field( $loop, $variation_data, $variation ) {
- $variation = wc_get_product( $variation );
- woocommerce_wp_text_input( array(
- 'id' => "stock_multiplier{$loop}",
- 'name' => "stock_multiplier[{$loop}]",
- 'value' => $variation->get_meta( '_stock_reducer' ),
- 'label' => __( 'Inventory reduction per variable item sold', 'woocommerce' ),
- 'placeholder' => __( 'When purchased, reduce stock level with this much', 'woocommerce' ),
- 'type' => 'number',
- 'custom_attributes' => array(
- 'min' => '1',
- 'step' => '1',
- ),
- ) );
- }
- add_action( 'woocommerce_variation_options_pricing', 'bb_add_stock_reducer_variable_product_field', 50, 3 );
- function bb_save_single_product_stock_reducer( $product ) {
- if ( ! empty( $_POST['_stock_reducer'] ) ) {
- $product->update_meta_data( '_stock_reducer', absint( $_POST['_stock_reducer'] ) );
- }
- }
- add_action( 'woocommerce_admin_process_product_object', 'bb_save_single_product_stock_reducer' );
- function bb_save_variable_product_stock_reducer( $variation_id, $i ) {
- $variation = wc_get_product( $variation_id );
- if ( ! empty( $_POST['stock_multiplier'] ) && ! empty( $_POST['stock_multiplier'][ $i ] ) ) {
- $variation->update_meta_data( '_stock_reducer', absint( $_POST['stock_multiplier'][ $i ] ) );
- $variation->save();
- }
- }
- add_action( 'woocommerce_save_product_variation', 'bb_save_variable_product_stock_reducer', 10, 2 );
- function bb_stock_reducer( $quantity, $order, $item ) {
- $multiplier = $item->get_product()->get_meta( '_stock_reducer' );
- if ( empty( $multiplier ) && $item->get_product()->is_type( 'variation' ) ) {
- $product = wc_get_product( $item->get_product()->get_parent_id() );
- $multiplier = $product->get_meta( '_stock_reducer' );
- }
- if ( ! empty( $multiplier ) ) {
- $quantity = $multiplier * $quantity;
- }
- return $quantity;
- }
- add_filter( 'woocommerce_order_item_quantity', 'bb_stock_reducer', 10, 3 );
Add Comment
Please, Sign In to add comment