wpgenie

set default qty for lotteries

Dec 11th, 2023 (edited)
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. add_action( 'woocommerce_product_options_lottery' , 'wpgenie_add_custom_lottery_quty_input');
  2. function wpgenie_add_custom_lottery_quty_input() {
  3.  
  4.     woocommerce_wp_text_input(
  5.                 array(
  6.                     'id'                => '_lottery_custom_qty_value',
  7.                     'class'             => 'input_text',
  8.                     'size'              => '6',
  9.                     'label'         => __( 'Default qty value', 'wc-lottery-pn' ),
  10.                     'type'              => 'number',
  11.                     'custom_attributes' => array(
  12.                         'step' => '1',
  13.                         'min'  => '0',
  14.                     ),
  15.                 )
  16.             );
  17. }
  18.  
  19. add_action( 'lottery_product_save_data', 'wpgenie_product_save', 30, 2  );
  20. function wpgenie_product_save( $post_id, $post ) {
  21.  
  22.     if ( isset( $_POST['_lottery_custom_qty_value'] ) ) {
  23.                 update_post_meta( $post_id, '_lottery_custom_qty_value', intval( $_POST['_lottery_custom_qty_value'] ) );
  24.     } else {
  25.         delete_post_meta( $post_id, '_lottery_custom_qty_value' );
  26.     }
  27. }
  28.  
  29. add_filter( 'woocommerce_quantity_input_args', 'wpgenie_woocommerce_quantity_input_args' , 10, 2);
  30. function wpgenie_woocommerce_quantity_input_args( $args, $product) {
  31.  
  32.     if ( is_product( ) ) {
  33.  
  34.         $post_id = $product->get_id();
  35.         $qty = get_post_meta( $post_id, '_lottery_custom_qty_value', true );
  36.  
  37.         if ( $product->get_type() == 'lottery' && ! empty( $qty ) ) {
  38.             if ( intval( $qty ) > intval( $product->get_max_purchase_quantity() ) ) {
  39.                 $args['input_value'] = intval( $product->get_max_purchase_quantity() );
  40.             } else {
  41.                 $args['input_value'] = intval( $qty );
  42.             }
  43.         }
  44.     }
  45.  
  46.     return $args;
  47.  
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment