我使用了以下代码,根据产品出售时作者的标签向产品添加了慈善标签

add_action( 'woocommerce_order_status_processing', 'update_product_meta', 20, 2 );
function update_product_meta ( $order_id, $order ) {
    $items = $order->get_items(); 
    foreach ( $order->get_items() as $item ) {
        $product_id = $item['product_id']
        $post_author = $product_id->post_author; 
        $product = wc_get_product( $product_id );
        $product-> update_meta_data( 'product_charity_tag', get_the_author_meta('charity_tag', $post_author ) );
        $product-> save();
    }
}

我想在特定月份的订单中检索与这些标签一起销售的产品. 我试过这样做;

首先获取特定时间段内的所有订单

 $orders = wc_get_orders(array(
    'post_type' => 'shop_order',
    'posts_per_page' => -1,
    'date_before' => $last_day_month,
    'date_after' => $first_day_month,
));

然后判断这些订单中的产品是否已贴上特定慈善机构的标签

 if(!empty($orders)){
        foreach ($orders as $order){
            foreach ($order->get_items() as $item ) {
                $product = $item->get_product(); 
                $product_data = $product->get_meta('product_charity_tag');
                if($product_data = 'bhf'){
                    if($all_products){
                        $all_products += $product;
                    } else {
                        $all_products = $product;
                    }
                }
                }
            }     
                 
            print_r($all_products); 
                 
     } else {
        print("No orders found");
    }     

但是我得到了以下致命错误:

未捕获类型错误:不支持的操作数类型:WC_Product_Simple+WC_Product_Simple

推荐答案

这个错误.未捕获类型错误:不支持的操作数类型:WC_Product_Simple+WC_Product_Simple. 这是因为您试图使用+=运算符将两个产品对象添加到一起,这在对象的PHP中是不受支持的.

您可以try 将产品收集到一个数组中.

add_action( 'woocommerce_order_status_processing', 'update_product_meta', 20, 2 );
function update_product_meta( $order_id, $order ) {
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id(); //  use get_product_id()
        $product = wc_get_product( $product_id );
        if ( $product ) {
            $post_author = get_post_field( 'post_author', $product_id ); // get post author
            $charity_tag = get_the_author_meta( 'charity_tag', $post_author );
            $product->update_meta_data( 'product_charity_tag', $charity_tag );
            $product->save();
        }
    }
}

to retrieve products sold with a specific charity tag with a date range

function get_charity_tagged_products( $first_day_month, $last_day_month, $charity_tag ) {
    $orders = wc_get_orders(array(
        'limit' => -1,
        'date_after' => $first_day_month,
        'date_before' => $last_day_month,
        'status' => 'completed', //if you have complete order status, change as needed
    ));

    $all_products = array(); // Initialize as array

    if ( !empty( $orders ) ) {
        foreach ( $orders as $order ) {
            foreach ( $order->get_items() as $item ) {
                $product = $item->get_product();
                if ( $product && $product->get_meta( 'product_charity_tag' ) == $charity_tag ) {
                    $all_products[] = $product; // Add product to array
                }
            }
        }
    }

    return $all_products; // Return the array of products
}

使用方法EX

$first_day_month = '2023-01-01';
$last_day_month = '2023-01-31';
$charity_tag = 'bhf';
$charity_products = get_charity_tagged_products($first_day_month, $last_day_month, $charity_tag);

Php相关问答推荐

我的docker—compose lamp stack在软件更新后不工作:Containerconfig错误'''

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

允许在WooCommerce管理员优惠券列表中显示自定义优惠券类型

如何在Livewire 3 Form类中做依赖注入?

首先在WooCommerce我的帐户订单页面中移动操作按钮

在Laravel中创建辅助函数时的约定

在WooCommercel邮箱通知上添加来自Apaczka插件的选定交付点

在 PHP 中将字符串分解为数组(字符串类似于 WP 短代码)

有没有办法像引用枚举一样引用注册表中的对象?

如何在 PHP 中以 hh:mm:ss 格式获取两个 strtotime datetime/s 之间的时间差

在 WooCommerce 中应用优惠券时显示购物车商品折扣金额

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

PHP Symfony 在测试.env文件中将接口自动装配作为构造函数参数,但使用接口的特定类

Shopware 6.4.20店铺激活问题

php/html OnSubmit 禁用输入类型提交未给出预期结果

htaccess php 文件,然后是文件夹

PDO 和 SQL 查询结果中日期格式不同的原因可能是什么?

无法在 Laravel 10 中安装 jenssegers/mongodb

Php 的新WeakMap - 枚举曾经被垃圾收集过吗?

图像不会显示在生产中的 Laravel 应用程序中