首页 > 解决方案 > WooCommerce 管理员订单添加可编辑元数据并保存

问题描述

我有一个在我的 WooCommerce Wordpress 网站前端运行的功能,它为正确回答简单问题的人生成票号,而为没有正确回答的人生成票号。它将用户答案保存在订单项目元数据中,我拥有它,以便答案显示在后端的可编辑 WooCommerce 文本输入字段中,用于订单中的每个项目,因为每个项目都有不同的答案。

add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values', 10, 3);
function pd_admin_order_item_values( $product, $item, $item_id ) {
    $user_answer = $item->get_meta( "User Answer" );
?>
<div class="edit_custom_field"> <!-- use same css class in h4 tag -->
<?php
    woocommerce_wp_text_input( array(
        'id' => 'custom_field_name',
        'label' => 'Update User Answer:',
        'value' => $user_answer,
        'wrapper_class' => 'form-field-wide'
    ) );
?>
</div>
<?php
}

我需要这样做,以便如果管理员将其更改为正确的字段并更新帖子,它将保存新数据并运行分配票号的功能。我使用了正确的钩子,我相信它会在我点击订单更新时崩溃,但日志显示它的参数太少而无法运行。我看到有些人使用 $post_id 和 $post 而不是订单变体,但将其更改为调用这些变量也没有奏效。

add_action( 'woocommerce_process_shop_order_meta', 'custom_woocommerce_process_shop_order_meta', 10, 4 );
function custom_woocommerce_process_shop_order_meta( $item, $order_id, $order ){
// Loop through order items
foreach ($item->get_items() as $item_id => $item ) {
    $admin_question = wc_sanitize_textarea( $_POST[ 'custom_field_name' ] );
    $user_answer = $item->get_meta( $admin_question );
    $admin_answer = $item->get_meta( "Answer" );
    if( $admin_answer == $user_answer ){
        // Check that tickets numbers haven't been generated yet for this item
        if( $item->get_meta( "_tickets_number") )
            continue;

        $product     = $item->get_product(); // Get the WC_Produt Object
        $quantity    = (int) $item->get_quantity(); // Get item quantity

        // Get last ticket sold index from product meta data
        $now_index = (int) $product->get_meta('_tickets_sold');

        $tickets_ids = array(); // Initializing

        // Generate an array of the customer tickets Ids from the product registered index (last ticket ID)
        for ($i = 1; $i <= $quantity; $i++) {
            $tickets_ids[] = $now_index + $i;
        };

        // Save the tickets numbers as order item custom meta data
        $item->update_meta_data( "Tickets numbers", implode('  ', $tickets_ids) ); // Displayed string of tickets numbers on customer orders and emails
        $item->update_meta_data( "User Answer", $user_answer ); // Displayed string of tickets numbers on customer orders and emails
        $item->update_meta_data( "_tickets_number", $tickets_ids ); // (Optional) Array of the ticket numbers (Not displayed to the customer)
        $item->save(); // Save item meta data

        // Update the Ticket index for the product (custom meta data)
        $product->update_meta_data('_tickets_sold', $now_index + $quantity );
        $product->save(); // Save product data
    } else {
        $item->update_meta_data( "User Answer", $user_answer ); // Displayed string of tickets numbers on customer orders and emails
        $item->save(); // Save item meta data
    }
}
$order->save(); // Save all order data
}

我认为我的问题在于它在运行函数时无法调用 order 和 order id 变量,但我不确定。

标签: phpwordpresswoocommerceorders

解决方案


事实证明,如果我将订单移回暂停状态,在那里编辑元数据,然后将其移至再次处理,它会运行原始功能并分配一个数字。


推荐阅读