【发布时间】:2021-06-17 19:13:55
【问题描述】:
我在 woocommerce 的结帐页面中添加了一个复选框。 如果这是商业订单,客户会检查并填写税号。
如果选中,我可以使用以下代码在数据库中获取“1”值。
但如果未单击复选框,我也希望看到“0”。
我该如何管理它?
add_filter('woocommerce_checkout_fields', 'commercial_order_checkout_field');
function commercial_order_checkout_field($fields)
{
$fields['billing']['commercial_order'] = array(
'type' => 'checkbox',
'priority' => 8,
'label' => __('Kurumsal Sipariş', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['tax_number'] = array(
'label' => __('Vergi Dairesi', 'woocommerce'),
'placeholder' => _x('Vergi Dairesi', 'placeholder', 'woocommerce'),
'class' => array('form-row form-row-first'),
'required' => true,
'priority' => 31,
'clear' => true
);
return $fields;
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) )
update_post_meta( $order_id, 'commercial_order', $_POST['commercial_order'] );
}
【问题讨论】:
-
看到这一行:
if ( ! empty( $_POST['my_field_name'] ) ) update_post_meta...它说'当不为空'.. 所以要回答你的问题,当它是空的时候做相反的操作
标签: woocommerce customization checkout