Search code examples
phpwordpressdatewoocommercecustom-fields

Set a default value for a custom date field in WooCommerce


I added a custom field date to my products variation and i want to set a default value for this field.

// 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]) );
    }
}

I want to set the default value to The next month. I tried to do this :

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

But it's not working. Anyone have a workaround to do this ?


Solution

  • You can set a default value for the input with a date inside the value attribute, like so:

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