wpgenie icon

set default qty for lotteries

wpgenie | PRO | 01/24/24 07:50:54 AM UTC (Edited) | 0 ⭐ | 1083 👁️ | Never ⏰ | []
PHP |

1.54 KB

|

None

|

0 👍

/

0 👎

add_action( 'woocommerce_product_options_lottery' , 'wpgenie_add_custom_lottery_quty_input');
function wpgenie_add_custom_lottery_quty_input() {
 
    woocommerce_wp_text_input(
                array(
                    'id'                => '_lottery_custom_qty_value',
                    'class'             => 'input_text',
                    'size'              => '6',
                    'label'         => __( 'Default qty value', 'wc-lottery-pn' ),
                    'type'              => 'number',
                    'custom_attributes' => array(
                        'step' => '1',
                        'min'  => '0',
                    ),
                )
            );
}
 
add_action( 'lottery_product_save_data', 'wpgenie_product_save', 30, 2  );
function wpgenie_product_save( $post_id, $post ) {
 
    if ( isset( $_POST['_lottery_custom_qty_value'] ) ) {
                update_post_meta( $post_id, '_lottery_custom_qty_value', intval( $_POST['_lottery_custom_qty_value'] ) );
    } else {
        delete_post_meta( $post_id, '_lottery_custom_qty_value' );
    }
}
 
add_filter( 'woocommerce_quantity_input_args', 'wpgenie_woocommerce_quantity_input_args' , 10, 2);
function wpgenie_woocommerce_quantity_input_args( $args, $product) {
 
    if ( is_product( ) ) {
 
        $post_id = $product->get_id();
        $qty = get_post_meta( $post_id, '_lottery_custom_qty_value', true );
 
        if ( $product->get_type() == 'lottery' && ! empty( $qty ) ) {
            if ( intval( $qty ) > intval( $product->get_max_purchase_quantity() ) ) {
                $args['input_value'] = intval( $product->get_max_purchase_quantity() );
            } else {
                $args['input_value'] = intval( $qty );
            }
        }
    }
 
    return $args;
 
}
 

Comments