WooCommerce自定义复选框结账时发布$_POST问题

时间:2018-08-13 作者:rward

我在结帐页面中添加了一个自定义复选框,并试图在订单页面上获得结果。下面是我试过的。当我提交订单时,即使勾选了,也总是会导致“不适用”。

提前感谢您的帮助。

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>\';
}

1 个回复
SO网友:rward

我找到了一个解决方案:

// 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>\';
}

结束

相关推荐