Skip to content

Instantly share code, notes, and snippets.

@mohsinworld
Last active June 12, 2024 10:21
Show Gist options
  • Save mohsinworld/d919cbd06574e9ae1354f6c2bfb12b30 to your computer and use it in GitHub Desktop.
Save mohsinworld/d919cbd06574e9ae1354f6c2bfb12b30 to your computer and use it in GitHub Desktop.
Add as PHP Snippet by WP Code Snippet Plugin. Insert Method: Auto Insert, Location: Run Everywhere.
// 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>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment