在 WooCommerce 中为自定义日期字段设置默认值

Set a default value for a custom date field in WooCommerce

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

// 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')))
        )
    );

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

您可以在 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 );