如何在WooCommerce Admin订单编辑页面中循环循环多个自定义字段

发布于 2025-01-21 16:05:36 字数 2126 浏览 0 评论 0原文

我有一个根据产品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.

loop attendee name custom field

show incremented custom field

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-01-28 16:05:36

有不同的方法,您可以使用$ order-&gt; get_meta_data()从当前$订单对象获取所有元数据。然后,您只需显示以给定子字符串开头的值。

有关您当前代码的一些注释

  • woocommerce_checkout_create_order挂钩用于对WooCommerce_Checkout_update_order_meta hook进行保存,因为$ $订单已经传递给回调功能
  • 建议在创建meta_keys时不要使用空间,这将减少错误负担

,因此您得到:

// Save meta data
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Count
    $count = 6;

    // Some value
    $value = 'the_value_';

    // Loop
    for ( $i = 1; $i <= $count; $i++ ) {
        // Update meta data
        $order->update_meta_data( 'name_of_attendee_' . $i, $value . $i );
    }
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Get all meta data.
    $meta_data = $order->get_meta_data();

    // Loop through data
    foreach ( $meta_data as $meta_data_obj ) {
        $meta_data_array = $meta_data_obj->get_data();

        // Checks if a string starts with a given substring
        if ( str_starts_with( $meta_data_array['key'], 'name_of_attendee_' ) ) {
            // Output value
            echo '<p>Attendee name: ' . $meta_data_array['value'] . '<p>'; 
        }
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

另一种方式是添加$ count值作为隐藏字段,并将其值作为订单元数据保存(除其他所需数据之外),

然后您可以为输出使用$ count

function action_woocommerce_before_order_notes( $checkout ) {
    // Count
    $count = 10;

    // Hidden field
    echo '<input type="hidden" name="_count" value="' . $count . '">';

    // Output of the other fields
    // etc...
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );

// Save
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset    
    if ( isset( $_POST['_count'] ) ) {    
        // Update meta data
        $order->update_meta_data( '_count', sanitize_text_field( $_POST['_count'] ) );
    }

    // Save the other fields
    // etc..
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Get count
    $count = $order->get_meta( '_count' );

    for ( $i = 1; $i <= $count; $i++ ) {
        echo '<p>Attendee Name... etc.. </p>';

        // Get meta from the other fields
        // $output = $order->get_meta( 'name_of_attendee_' . $i );
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

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

  • The woocommerce_checkout_create_order hook was used to save against the woocommerce_checkout_update_order_meta hook because the $order object is already passed to the callback function
  • It is recommended not to use spaces when creating meta_keys, this will reduce the error burden

So you get:

// Save meta data
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Count
    $count = 6;

    // Some value
    $value = 'the_value_';

    // Loop
    for ( $i = 1; $i <= $count; $i++ ) {
        // Update meta data
        $order->update_meta_data( 'name_of_attendee_' . $i, $value . $i );
    }
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Get all meta data.
    $meta_data = $order->get_meta_data();

    // Loop through data
    foreach ( $meta_data as $meta_data_obj ) {
        $meta_data_array = $meta_data_obj->get_data();

        // Checks if a string starts with a given substring
        if ( str_starts_with( $meta_data_array['key'], 'name_of_attendee_' ) ) {
            // Output value
            echo '<p>Attendee name: ' . $meta_data_array['value'] . '<p>'; 
        }
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

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

function action_woocommerce_before_order_notes( $checkout ) {
    // Count
    $count = 10;

    // Hidden field
    echo '<input type="hidden" name="_count" value="' . $count . '">';

    // Output of the other fields
    // etc...
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );

// Save
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset    
    if ( isset( $_POST['_count'] ) ) {    
        // Update meta data
        $order->update_meta_data( '_count', sanitize_text_field( $_POST['_count'] ) );
    }

    // Save the other fields
    // etc..
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Get count
    $count = $order->get_meta( '_count' );

    for ( $i = 1; $i <= $count; $i++ ) {
        echo '<p>Attendee Name... etc.. </p>';

        // Get meta from the other fields
        // $output = $order->get_meta( 'name_of_attendee_' . $i );
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文