WooCommerce Custom Delivery Days

 avatar
mohsinworld
plain_text
18 days ago
2.0 kB
2
Indexable
Never
// Add a custom delivery days range field to the product editing page
add_action('woocommerce_product_options_general_product_data', 'add_custom_delivery_days_range_field');

function add_custom_delivery_days_range_field() {
    global $post;

    // Get the current value, if it exists
    $custom_delivery_days = get_post_meta($post->ID, '_custom_delivery_days', true);
    
    echo '<div class="options_group">';
    
    // Custom Delivery Days Range Field
    woocommerce_wp_text_input(
        array(
            'id'          => '_custom_delivery_days',
            'label'       => __('Delivery Days', 'woocommerce'),
            'placeholder' => 'e.g., 4 or 3-7',
            'desc_tip'    => 'true',
            'description' => __('Enter the range of days required for delivery (e.g., 4 or 3-7).', 'woocommerce'),
            'type'        => 'text',
            'value'       => $custom_delivery_days
        )
    );
    
    echo '</div>';
}


// Save the custom delivery days field value
add_action('woocommerce_process_product_meta', 'save_custom_delivery_days_field');

function save_custom_delivery_days_field($post_id) {
    // Custom Delivery Days
    $custom_delivery_days = isset($_POST['_custom_delivery_days']) ? sanitize_text_field($_POST['_custom_delivery_days']) : '';
    if ($custom_delivery_days === '') {
        delete_post_meta($post_id, '_custom_delivery_days');
    } else {
        update_post_meta($post_id, '_custom_delivery_days', $custom_delivery_days);
    }
}

// Display the delivery days on the product page after the price
add_action('woocommerce_single_product_summary', 'display_custom_delivery_days_after_price', 11);

function display_custom_delivery_days_after_price() {
    global $post;
    
    $custom_delivery_days = get_post_meta($post->ID, '_custom_delivery_days', true);
    
    if (!empty($custom_delivery_days)) {
        echo '<p class="delivery-days">' . __('Delivery Time: ', 'woocommerce') . esc_html($custom_delivery_days) . __(' Working Days', 'woocommerce') . '</p>';
    }
}
Leave a Comment