我使用此代码片段在 woocommerce 结账表单中添加自定义字段。但该字段被添加到最后部分。我需要在电话字段之后显示该字段。而且它必须是必填字段
/*** Add custom field to the checkout page ***/
add_action('woocommerce_after_order_notes', 'custom_checkout_field');
function custom_checkout_field($checkout) {
woocommerce_form_field('billing_phone', array(
'type' => 'number',
'class' => array(
'my-field-class form-row-wide'
),
'label' => __('Phone 2'),
'placeholder' => __(''),
),
$checkout->get_value('billing_phone'));
echo '</div>';
}
这是添加自定义字段的部分
必须在此电话字段下方添加自定义字段
我必须在这里改变什么来解决这个问题。等待回复...
您已将代码添加到“woocommerce_after_order_notes”操作中。它确实做到了。您必须将其更改为“woocommerce_after_checkout_billing_form”。 这是 businessbloomer 的帖子,解释了职位的位置。
这是固定代码。
add_action('woocommerce_after_checkout_billing_form', 'custom_checkout_field');
// for the shipping form
add_action('woocommerce_after_checkout_shiiping_form', 'custom_checkout_field');
function custom_checkout_field($checkout) {
woocommerce_form_field('billing_phone', array(
'type' => 'number',
'class' => array(
'my-field-class form-row-wide'
),
'label' => __('Phone 2'),
'placeholder' => __(''),
),
$checkout->get_value('billing_phone'));
echo '</div>';
}