猿问

从 WooCommerce 中的自定义结帐计费字段获取价值

我在woocommerce 文档后面的结帐处添加了一个字段,如下所示:

/*Add document ID to checkout form*/

add_filter( 'woocommerce_checkout_fields' , 'ebani_cedula_checkout_field' );


// Our hooked in function - $fields is passed via the filter!

function ebani_cedula_checkout_field( $fields ) {

    $fields['billing']['cedula'] = array(

        'label'     => __('Cédula de ciudadanía', 'woocommerce'),

        'placeholder'   => _x('Cédula', 'placeholder', 'woocommerce'),

        'required'  => true,

        'class'     => array('form-row-last'),

        'clear'     => true,

        'priority' => 15

    );


     return $fields;

}

然后我想以这种方式在管理订单编辑页面上显示它:


/**

 * Display field value on the order edit page

 */

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

    

 function cedula_checkout_field_display_admin_order_meta($order){

        echo '<p><strong>'.__('C&eacute;dula').':</strong> ' . get_post_meta( $order->get_id(), '_cedula', true ) . '</p>';

 }

当我进入订单编辑页面时,我得到一个空值,_cedula我不知道为什么,我只是按照文档操作,但它不起作用,如何获取存储在自定义结账字段中的数据?


千万里不及你
浏览 121回答 1
1回答

Smart猫小萌

如果您希望在下订单时保存此自定义账单字段,最好使用操作挂钩,woocommerce_billing_fields而不是woocommerce_checkout_fields像:add_filter( 'woocommerce_billing_fields' , 'ebani_cedula_checkout_field' );function ebani_cedula_checkout_field( $fields ) {&nbsp; &nbsp; $fields['billing_cedula'] = array(&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp;=> __('Cédula de ciudadanía', 'woocommerce'),&nbsp; &nbsp; &nbsp; &nbsp; 'placeholder' => _x('Cédula', 'placeholder', 'woocommerce'),&nbsp; &nbsp; &nbsp; &nbsp; 'required'&nbsp; &nbsp; => true,&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp; &nbsp;=> array('form-row-last'),&nbsp; &nbsp; &nbsp; &nbsp; 'clear'&nbsp; &nbsp; &nbsp; &nbsp;=> true,&nbsp; &nbsp; &nbsp; &nbsp; 'priority'&nbsp; &nbsp; => 15&nbsp; &nbsp; );&nbsp; &nbsp; return $fields;}add_action( 'woocommerce_admin_order_data_after_billing_address', 'cedula_checkout_field_display_admin_order_meta', 10, 1 );function cedula_checkout_field_display_admin_order_meta($order){&nbsp; &nbsp; if( $value = $order->get_meta('_billing_cedula') )&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>'.__('Cédula').':</strong> ' . $value . '</p>';}现在您的自定义结帐账单字段已保存。
随时随地看视频慕课网APP
我要回答