【问题标题】:WooCommerce lottery pick number: Add leading zeros to lottery numbers [closed]WooCommerce 彩票号码:在彩票号码中添加前导零 [关闭]
【发布时间】:2021-03-04 11:25:33
【问题描述】:
  1. 我想编辑我的付费 WooCommerce 插件。它会生成 1、2、3、4 的彩票号码,我希望它有 4 位数字,例如 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();
         }
     }
    

【问题讨论】:

    标签: php wordpress woocommerce plugins numbers


    【解决方案1】:

    您的问题过于抽象,但您可以使用 WordPress 内置格式化功能zeroise() 来实现您想要的输出。

    WordPress zeroise() function的官方文档

    WordPress (版本 5.5.3) zeroise() function的源代码

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多