0

I've added a custom checkbox to my checkout page and trying to get results onto order page. Below is what I've tried. When I submit an order it always results in "N/A" even if checked.

Thank you in advance for your help.

add_action( 'woocommerce_review_order_before_submit', 'add_checkout_sign_optout_policy', 9 );

function add_checkout_sign_optout_policy() {

woocommerce_form_field( 'nosign', array(
'type'          => 'checkbox',
'class'         => array('form-row nosign'),
'label_class'   => array('woocommerce-form__label woocommerce-form__label-for- checkbox checkbox'),
'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox'),
'required'      => false,
'label'         => 'I Opt-Out of Signature Upon Delivery and understand all <a href="" class="woocommerce-shipping-optout-link">Terms</a>.',
)); 

}

/* Save "Terms and Conditions" */

// 1. Save Opt-Out as Order Meta

add_action( 'woocommerce_checkout_update_order_meta', 'save_optout_acceptance' 
);

function save_optout_acceptance( $order_id ) {
if ( $_POST['nosign'] ) update_post_meta( $order_id, 'nosign', esc_attr( $_POST['nosign'] ) );
}

// 2. Display Opt-Out Single Order Page 

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_optout_acceptance' );

function display_optout_acceptance( $order ) {
if ( get_post_meta( $order->get_id(), 'nosign', true ) == 'on' ) {
    echo '<p><strong>Shipping Opt-Out: </strong>accepted</p>';
} else echo '<p><strong>Shipping Opt-Out: </strong>N/A</p>';
}
0

I have found a solution:

// 2. Display T&C @ Single Order Page 

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_optout_acceptance', 10, 1 );

function display_optout_acceptance( $order ) {
$nosignoptout = get_post_meta( $order->get_id(), 'nosign', true );
if ( $nosignoptout == 1) {
    echo '<p><strong>Shipping Opt-Out: </strong>Selected</p>';
} else echo '<p><strong>Shipping Opt-Out: </strong>Not Selected</p>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.