我正试图使用一个钩子更新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');
        }
    }
}

推荐答案

您的代码正在try 访问Order属性,如下所示

$order->id

而不是

$order->get_id()

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

Php相关问答推荐

Laravel:启动下载并同时在Blade 中显示Flash消息

WooCommerce中仅在单一产品和产品档案页面中将小数设置为大写

Mysqli_stmt::Execute():结果字段元数据中存在过早的EOF

使用正则表达式搜索两个子字符串(没有重叠/共享字符)

创建一个新的可变产品及其属性以用于WooCommerce中的变体

在PHP中循环访问多维数组,并按组显示内容

如果为布尔值,则 for each 循环重新启动

Select 本地提货时隐藏WooCommerce显示的发货成本

Laravel自定义验证规则更正

Laravel Http::get not working on controller

添加不存在的订单元数据以扩展WooCommerce针对特定产品的管理订单搜索

在WooCommerce循环中添加子类别和产品之间的分隔符

如何将不同的 Woocommerce 费用合并为一个名称?

获取 Shopware 6 集合元素的值,PHP

如何使用相同的变量使函数参数对于数组和字符串都是可选的

PHP / DOM : 解析 HTML 以提取基于类的数​​据

从 CSV 文件 seeder 数据库时的额外行

如果 static:: 被解析为B,为什么 PHP 会执行A的方法

一个php应用程序可以实例化多个RNG吗

无法按今天加上 7 天显示数据库项目