php - 在 woocommerce 3 中获取自定义结帐字段的值
问题描述
在 Woocommerce 结帐中,我添加了一个自定义结帐字段,这是我的代码:
add_action( 'woocommerce_before_order_notes', 'shipping_add_select_checkout_field' );
function shipping_add_select_checkout_field( WC_Checkout $checkout ) {
$options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
woocommerce_form_field( 'billing_country_zone', array(
'type' => 'select',
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
'label' => __( 'City zone' ),
'required' => true,
'options' => $options
), WC()->customer->billing_country_zone );
}
现在我完全迷失了,因为我需要知道它WC()->customer->billing_country_zone
的用途以及如何检查它的价值……
非常感谢任何帮助。
解决方案
对于WC()->customer->billing_country_zone
:
- 首先,自 Woocommerce 3 以来,可以在大多数 Woocommerce 实例对象上访问属性。
- 并且“
billing_country_zone
”不是WC_Customer
实例对象的默认属性。
由于它是关于结帐字段,因此您应该使用作为对象$checkout
实例的参数WC_Checkout
。然后有适当的方法get_value()
可以用于它......
那个有什么用?
一旦客户提交了至少一个订单,“billing_country_zone
”的选定值将显示在结帐页面上。
因此,您将不得不替换该行:
), WC()->customer->billing_country_zone );
通过这个:
), $checkout->get_value('billing_country_zone') );
如果
$checkout
未定义变量参数,您将使用WC()->checkout
如下:), WC()->checkout->get_value('billing_country_zone') );
现在,当您保存此自定义结帐字段值时,您需要保存它:
- 作为订单元数据
- 也作为用户元数据
所以这是完整的代码(注释):
// Display custom checkout field
add_action( 'woocommerce_before_order_notes', 'display_custom_checkout_field' );
function display_custom_checkout_field( $checkout ) {
$options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
woocommerce_form_field( 'billing_country_zone', array(
'type' => 'select',
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
'label' => __( 'City zone' ),
'required' => true,
'options' => $options
), $checkout->get_value('billing_country_zone') );
}
// custom checkout field validation
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
function custom_checkout_field_validation() {
if ( isset( $_POST['billing_country_zone'] ) && empty( $_POST['billing_country_zone'] ) )
wc_add_notice( __( 'Please select a <strong>"City zone"</strong>.', 'woocommerce' ), 'error' );
}
// Save custom checkout field value as custom order meta data and user meta data too
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 20, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
if ( isset( $_POST['billing_country_zone'] ) ) {
// Save custom checkout field value
$order->update_meta_data( '_billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
// Save the custom checkout field value as user meta data
if( $order->get_customer_id() )
update_user_meta( $order->get_customer_id(), 'billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。
推荐阅读
- asynchronous - 我应该在 Rust 中返回 await 吗?
- flutter - 如何在行中制作灵活的多个小部件?
- angular - 为什么这个 app-transition-group 和 app-transition-group-item 自定义组件在 angular8 中不起作用(与 angular2 一起使用)?
- php - Laravel 计算并忽略空值
- c++ - C ++ Map插入具有重复键的对
- excel - 使用 VBA 控制 Excel 数据透视表字段列表中元素的顺序
- python - python中的行梯队
- python - 在seaborn分类图中绘制类别意味着
- javascript - 我无法使用 express 在节点 js 中创建虚拟路径前缀(文件系统中实际上不存在该路径)
- powershell - 如何检查powershell中的参数数量?