WooCommerce lottery pick number: Add leading zeros to lottery numbers

Viewed 253
  1. I want to edit my paid WooCommerce plugin. It generates lottery numbers in 1, 2, 3, 4, and I want it in 4 digits like 0001, 0002, 0003, 0004.

     function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
         if ( is_null( $product ) ) {
             $product = $GLOBALS['product'];
         }
    
         $defaults = array(
             'input_id'    => uniqid( 'quantity_' ),
             'input_name'  => 'quantity',
             'input_value' => '1',
             'classes'      => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
             'max_value'   => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
             'min_value'   => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
             'step'        => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
             'pattern'     => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
             'inputmode'   => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
         );
    
         $args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
    
         // Apply sanity to min/max args - min cannot be lower than 0.
         $args['min_value'] = max( $args['min_value'], 0 );
         $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : '';
    
         // Max cannot be lower than min if defined.
         if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
             $args['max_value'] = $args['min_value'];
         }
    
         ob_start();
    
         if ( get_post_meta( $product->get_id() , '_lottery_use_pick_numbers', true ) === 'yes' && "yes" !== get_post_meta( $product->get_id() , '_lottery_pick_numbers_random', true ) && $args['input_id'] != 'qty_dip') {
             echo '<div class="quantity">
                 <input type="hidden" id="' .esc_attr( $args['input_id'] ) . '" class="qty" name="' . esc_attr( $args['input_name'] ) . '" value="' . esc_attr($args['input_value']) . '" />
                 '.$args['input_value'].'
             </div>';
         } else {
             wc_get_template( 'global/quantity-input.php', $args );
         }
    
    
         if ( $echo ) {
             echo ob_get_clean(); // WPCS: XSS ok.
         } else {
             return ob_get_clean();
         }
     }
    
1 Answers

Your question is too abstract but you can achieve your desired output using WordPress inbuilt formatting function zeroise().

Official documentation for WordPress zeroise() function

WordPress (version 5.5.3) source code for zeroise() function

Related