Skip to content

Instantly share code, notes, and snippets.

@BrianHenryIE
Last active February 18, 2018 12:35
Show Gist options
  • Save BrianHenryIE/a4e96763a0f7fd771e071a947fdf5116 to your computer and use it in GitHub Desktop.
Save BrianHenryIE/a4e96763a0f7fd771e071a947fdf5116 to your computer and use it in GitHub Desktop.
Adds Expected Delivery Time meta box in WooCommerce Product page Shipping options
<?php
// Save in your child theme and add in your functions.php:
// require_once('bh_expected_delivery_time.php');
add_action( 'woocommerce_product_options_shipping', 'bh_expected_delivery_time_field' );
function bh_expected_delivery_time_field() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_bh_expected_delivery_time',
'label' => 'Expected delivery time',
'description' => 'Estimated shipping time in days. To be displayed, separately, prominently from the main description.',
'desc_tip' => 'true',
'placeholder' => 'Days'
) );
}
add_action( 'woocommerce_process_product_meta', 'bh_expected_delivery_time_save_field' );
function bh_expected_delivery_time_save_field( $post_id ) {
if ( ! empty( $_POST['_bh_expected_delivery_time'] ) ) {
update_post_meta( $post_id, '_bh_expected_delivery_time', esc_attr( $_POST['_bh_expected_delivery_time'] ) );
}
}
add_shortcode('expected_delivery_time', 'bh_expected_delivery_time');
function bh_expected_delivery_time( $atts = array() ) {
if( is_int( $atts ) )
$post_id = $atts;
elseif( is_array( $atts ) && array_key_exists( $atts, 'post_id' ) )
$post_id = $atts['post_id'];
else
$post_id = get_the_ID();
$expected_delivery_time_meta = get_post_meta($post_id, '_bh_expected_delivery_time', true);
return $expected_delivery_time_meta;
}
// Use in visual editor:
// [expected_delivery_time]
// [expected_delivery_time post_id=123]
// Use in theme:
// echo bh_expected_delivery_time();
// echo bh_expected_delivery_time( 123 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment