【问题标题】:Set a default value for a custom date field in WooCommerce在 WooCommerce 中为自定义日期字段设置默认值
【发布时间】:2021-07-10 22:11:35
【问题描述】:

我为我的产品变体添加了一个自定义字段日期,我想为此字段设置一个默认值。

// Admin Variation custom fields
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 );
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';
    

    // Date de livraison estimée
    woocommerce_wp_text_input(
        array(
            'id'          => '_ab_preorder_estimated_date['.$loop.']',
            'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "Date de livraison estimé", "woocommerce" ),
            'type'        => 'date',
            'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
        )
    );

    echo '</div>';
}

// Save admin Variations custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'ab_preorder_variation_fields_saving', 10, 2 );
function ab_preorder_variation_fields_saving( $variation, $loop ) {


    if( isset($_POST['_ab_preorder_estimated_date'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_estimated_date', esc_attr($_POST['_ab_preorder_estimated_date'][$loop]) );
    }
}

我想将默认值设置为下个月。我试着这样做:

// Date de livraison estimée
    woocommerce_wp_text_input(
        array(
            'id'          => '_ab_preorder_estimated_date['.$loop.']',
            'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "Date de livraison estimé", "woocommerce" ),
            'type'        => 'date',
            'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true ),
            'default' => date('Y/m/d', strtotime('+1 month', date('Y/m/d')))
        )
    );

但它不起作用。有人有解决方法吗?

【问题讨论】:

    标签: php wordpress date woocommerce custom-fields


    【解决方案1】:

    您可以在 value 属性中使用日期为输入设置默认值,如下所示:

    // Admin Variation custom fields
    function action_woocommerce_product_after_variable_attributes( $loop, $variation_data, $variation ) {
        // Get post meta
        $ab_preorder_estimated_date = get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true );
        
        // Empty
        if ( empty ( $ab_preorder_estimated_date ) ) {
            // Today
            $today = date( 'Y-m-d' );
            
            $value = date( 'Y-m-d', strtotime( $today . '+1 month' ) );
        } else {
            $value = $ab_preorder_estimated_date;
        }
    
        echo '<div class="options_group form-row form-row-full">';
    
        // Date de livraison estimée
        woocommerce_wp_text_input(
            array(
                'id'          => '_ab_preorder_estimated_date['.$loop.']',
                'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
                'desc_tip'    => true,
                'description' => __( "Date de livraison estimé", "woocommerce" ),
                'type'        => 'date',
                'value'       => $value,
            )
        );
    
        echo '</div>';
    }
    add_action( 'woocommerce_product_after_variable_attributes', 'action_woocommerce_product_after_variable_attributes', 10, 3 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2013-12-10
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多