在下面的代码中,我在结帐订单页面中显示自定义字段。
add_action('woocommerce_after_order_notes', 'custom_checkout_placeholder');
function custom_checkout_placeholder($checkout)
{
echo '<div id="customise_checkout_field">';
woocommerce_form_field('customised_field', array(
'type' => 'select',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Rate our support') ,
'placeholder' => __('') ,
'options' => array( __('Bad'), __('Good'), __('Very Good')),
) , $checkout->get_value('customised_field'));
echo '</div>';
}
现在我想在管理区域中显示此字段。
以下代码无法正常运行。在管理员中显示“评价我们的支持”标签,但不是用户的答案:
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'checkout_field_admin_order', 10, 1 );
function checkout_field_admin_order( $order ){
$customised_field = get_post_meta( $order->get_id(), 'customised_field', true );
echo '<p>'.__('Rate our support', 'woocommerce').': ' . $customised_field . '</p>';
}
为什么管理区域中的代码不起作用的任何想法?
答案 0 :(得分:0)
缺少的部分是:
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! isset( $_POST['customised_field'] ) ) return;
if ( empty( $_POST['customised_field'] ) ) return;
update_post_meta( $order_id, '_customised_field', sanitize_text_field( $_POST['customised_field'] ) );
}
在你的拉伸功能中,你将拥有:
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'checkout_field_admin_order', 10, 1 );
function checkout_field_admin_order( $order ){
$customised_field = get_post_meta( $order->get_id(), '_customised_field', true );
echo '<p>'.__('Rate our support', 'woocommerce').': ' . $customised_field . '</p>';
}
更新:也会将您的第一个功能更改为:
add_action('woocommerce_after_order_notes', 'custom_checkout_placeholder');
function custom_checkout_placeholder($checkout)
{
echo '<div id="customise_checkout_field">';
woocommerce_form_field('customised_field', array(
'type' => 'select',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Rate our support') ,
'placeholder' => __('') ,
'options' => array(
__('Bad') => __('Bad'),
__('Good') => __('Good'),
__('Very Good') => __('Very Good')
),
) , $checkout->get_value('customised_field'));
echo '</div>';
}
代码放在活动子主题(或活动主题)的function.php文件中。