|
<?php |
|
/* |
|
Add custom delivery radio buttons to checkout. |
|
Requires WooCommerce v3+ |
|
|
|
Add your radio buttons. |
|
Remove the 'required' attribute if these are to be optional. |
|
*/ |
|
add_action( 'woocommerce_review_order_before_payment', 'display_extra_fields_after_billing_address' , 10, 1 ); |
|
function display_extra_fields_after_billing_address () { ?> |
|
<h3>Delivery Options <sup>*</sup></h3> |
|
<p><input type="radio" name="delivery_option" value="Collect from the depot." required /> Collect from the depot.</p> |
|
<p><input type="radio" name="delivery_option" value="Leave the consignment without a signature." /> Leave the consignment without a signature.</p> |
|
<?php |
|
} |
|
|
|
add_action( 'woocommerce_checkout_update_order_meta', 'add_delivery_option_to_order' , 10, 1); |
|
function add_delivery_option_to_order ( $order_id ) { |
|
if ( isset( $_POST ['delivery_option'] ) && '' != $_POST ['delivery_option'] ) { |
|
add_post_meta( $order_id, '_delivery_option', sanitize_text_field( $_POST ['delivery_option'] ) ); |
|
} |
|
} |
|
|
|
/* |
|
Include selected option in notification emails. |
|
*/ |
|
add_filter( 'woocommerce_email_order_meta_fields', 'add_delivery_option_to_emails' , 10, 3 ); |
|
function add_delivery_option_to_emails ( $fields, $sent_to_admin, $order ) { |
|
|
|
if( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) { |
|
$order_id = $order->get_id(); |
|
} else { |
|
$order_id = $order->id; |
|
} |
|
|
|
$delivery_option = get_post_meta( $order_id, '_delivery_option', true ); |
|
|
|
if ( '' != $delivery_option ) { |
|
$fields[ 'Delivery Date' ] = array( |
|
'label' => __( 'Delivery Option', 'delivery_option' ), |
|
'value' => $delivery_option, |
|
); |
|
} |
|
return $fields; |
|
} |
|
|
|
/* |
|
Validation nag. |
|
Remove this if the fields are to be optional. |
|
*/ |
|
function action_woocommerce_after_checkout_validation( $data, $errors ) { |
|
if ( empty( $_POST['delivery_option'] ) ) : |
|
$errors->add( 'required-field', __( 'You have not chosen a delivery option.', 'woocommerce' ) ); |
|
endif; |
|
} |
|
add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 ); |
|
|
|
|
|
/* |
|
Display field value on the order edit page. |
|
*/ |
|
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); |
|
function my_custom_checkout_field_display_admin_order_meta($order) { |
|
echo '<p><strong>'.__('Delivery Option').':</strong> <br/>' . get_post_meta( $order->get_id(), '_delivery_option', true ) . '</p>'; |
|
} |
@brock39 Your link is a 404