wordpress WooCommerce -函数ID被错误调用,不应直接访问订单属性

e5nqia27  于 2023-06-21  发布在  WordPress
关注(0)|答案(1)|浏览(151)

我试图使用钩子更新WooCommerce中的订单状态,但我收到了一个PHP通知,内容如下
Notice: Function ID was called incorrectly. Order properties should not be accessed directly. auto_complete_specific_product_order, WC_Order->update_status, WC_Order->save, WC_Order->status_transition, do_action('woocommerce_order_status_completed'), WP_Ho in C:\Websites\mysite\www\wp-includes\functions.php on line 5865
这是我的函数,它似乎触发了PHP通知。我不能确切地看到我做错了什么,所以任何意见或建议将不胜感激。

// Hook the function to the order status change specifically for "completed"
add_action('woocommerce_order_status_completed', 'transfer_product_status_complete', 10, 2);
function transfer_product_status_complete($order_id)
{
    global $wpdb;

    // Retrieve the order
    $order = wc_get_order($order_id);

    // Check if the desired product is in the order
    $found = false;

    foreach ($order->get_items() as $item)
    {
        $product_id = $item->get_product_id();
        
        if ($product_id === 825 || $product_id === 885)
        {
            $found = true;
            break;
        }
    }

    // Execute the code only if the desired product is found
    if ($found)
    {
        // Retrieve the order user ID
        $order = wc_get_order($order_id);
        $new_owner_id = $order->get_user_id();

        // Retrieve and update custom fields when order becomes complete
        $serial_number_transfer = get_post_meta($order_id, 'serial_to_be_transferred', true);

        if (metadata_exists('post', $order_id, 'serial_to_be_transferred'))
        {
            // save the serial number
            update_post_meta($order_id, 'hash_data', $serial_number_transfer);

            // remove the serial from the custom field of the old owner 
            $orders = wc_get_orders(array(
                'customer' => $current_owner_id,
                'limit' => -1, // Retrieve all orders
            ));

            foreach ($orders as $order)
            {
                $order_id_sender = $order->ID;
                $serial_numbers = get_post_meta($order_id_sender, 'hash_data');

                $new_meta_value = str_replace($serial_number_transfer, '', $serial_numbers[0]);

                // Update the post meta with the new value
                update_post_meta($order_id_sender, 'hash_data', $new_meta_value);
            }

            // delete the custom field called serial_to_be_transferred
            delete_post_meta($order_id, 'serial_to_be_transferred');
        }
    }
}
hmae6n7t

hmae6n7t1#

您的代码在某个地方试图访问一个order属性,如

$order->id

而不是

$order->get_id()

您需要查看堆栈跟踪,找到发生这种情况的位置,并相应地修复它。我不知道这是id还是其他属性,因为它没有发生在您共享的代码块中。如果您需要进一步的帮助,您将需要共享更多的代码,特别是在试图直接访问订单属性的情况下。

相关问题