I have a small problem with my woocommerce v3.3.5 project. In my checkout I added custom select with the following options: delivery by email, delivery by post.
I have also created several delivery methods
3 for delivery by post
+1 for free by email.
Is it possible to dynamically change the displayed delivery methods based on the option selected in the custom field added?
Here is the code fragment for my custom select field
add_action( 'woocommerce_checkout_before_billing', 'before_billing_fields', 20 );
function before_billing_fields(){
$checkout = WC()->checkout;
woocommerce_form_field('delivery_method', array(
'type' => 'select',
'options' => array(
'blank' => __( 'Select a delivery method', 'sdm' ),
'shipping-by-post' => __( 'Shipping by post', 'sdm' ),
'shipping-by-email' => __( 'Shipping by email', 'sdm' )
),
'class' => array('delivery_method form-row-wide'),
'clear' => true
), $checkout->get_value('delivery_method'));
}
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
global $woocommerce;
if ($_POST['delivery_method'] == "blank")
wc_add_notice( '<strong>Please select a Delivery method</strong>', 'error' );
}
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
$keys['Delivery Method'] = 'delivery_method';
return $keys;
}
add_action('woocommerce_checkout_update_order_meta', 'checkout_update_order_meta');
function checkout_update_order_meta($order_id)
{
$delivery_method = $_POST['delivery_method'];
if (!empty($delivery_method))
update_post_meta($order_id, 'delivery_method', sanitize_text_field($delivery_method));
}
...and shipping methods: Woocommerce Shipping Methods