如何在WooCommerce Admin订单编辑页面中循环循环多个自定义字段
我有一个根据产品ID创建的WooCommerce自定义字段,并且该字段名称由1通过购买的数量来递增。
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
/**
* Save value of fields
*/
function customise_checkout_field_update_order_meta($order_id) {
// True
if ( WC()->cart ) {
// Specific product IDs
$product_ids = array( 11,12,13 );
// Initialize
$count = 0;
// Loop trough cart items quantities
foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
// Checks if a value exists in an array
if ( in_array( $product_id, $product_ids ) ) {
$count += $cart_item_quantity;
}
}
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Name of Attendee '.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
}
}
}
效果很好,但是我想在WooCommerce内部订单管理屏幕上显示字段,我如何循环遍历自定义字段并显示增量字段名称...
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_attendees_field_display_admin_order_meta', 10, 1 );
/**
* Display attendees value on the backend WooCommerce order
*/
function bt_attendees_field_display_admin_order_meta($order){
$i = 1;
echo '<p>Attendee Name ' . get_post_meta( $order->get_id(), 'Name of Attendee '.$i, true ) . '<p>';
}
因此,由于变量已已被变量,因此第一个字段上方将在上方输出设置为1-我如何通过每个订单的不确定数量相同的字段循环。
I have a WooCommerce custom field created dependent on product ID and the field name is incremented by 1 by quantity purchased.
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
/**
* Save value of fields
*/
function customise_checkout_field_update_order_meta($order_id) {
// True
if ( WC()->cart ) {
// Specific product IDs
$product_ids = array( 11,12,13 );
// Initialize
$count = 0;
// Loop trough cart items quantities
foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
// Checks if a value exists in an array
if ( in_array( $product_id, $product_ids ) ) {
$count += $cart_item_quantity;
}
}
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Name of Attendee '.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
}
}
}
That works fine, but I want to display the fields on the WooCommerce internal Order Admin screen, how can I loop through the custom field and display the incremented field names...
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_attendees_field_display_admin_order_meta', 10, 1 );
/**
* Display attendees value on the backend WooCommerce order
*/
function bt_attendees_field_display_admin_order_meta($order){
$i = 1;
echo '<p>Attendee Name ' . get_post_meta( $order->get_id(), 'Name of Attendee '.$i, true ) . '<p>';
}
So above the first field will be output above as the variable has been set to 1 - how can I loop though an indeterminate amount of the same field per order per order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有不同的方法,您可以使用
$ order-&gt; get_meta_data()
从当前$订单
对象获取所有元数据。然后,您只需显示以给定子字符串开头的值。有关您当前代码的一些注释
woocommerce_checkout_create_order
挂钩用于对WooCommerce_Checkout_update_order_meta
hook进行保存,因为$ $订单
已经传递给回调功能meta_keys
时不要使用空间,这将减少错误负担,因此您得到:
另一种方式是添加
$ count
值作为隐藏字段,并将其值作为订单元数据保存(除其他所需数据之外),然后您可以为输出使用
$ count
值There are different ways, you could use
$order->get_meta_data()
to get all meta data from the current$order
object. Then you just display the values that starts with a given substring.Some notes about your current code
woocommerce_checkout_create_order
hook was used to save against thewoocommerce_checkout_update_order_meta
hook because the$order
object is already passed to the callback functionmeta_keys
, this will reduce the error burdenSo you get:
Another way is to add the
$count
value as a hidden field and save its value as order meta data (in addition to the other desired data)Then you can use the
$count
value for the output