I have used the following code to add a charity_tag to products based on the tag of the author of the product when sold
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();
}
}
I would like to retrieve the products sold with these tags within orders over specific months.
I have tried doing it like this;
First get all orders within a specific time period
$orders = wc_get_orders(array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'date_before' => $last_day_month,
'date_after' => $first_day_month,
));
Then check if the products within these orders have been tagged with a specific charity
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");
}
However i get the following fatal error:
Uncaught TypeError: Unsupported operand types: WC_Product_Simple + WC_Product_Simple
2
Answers
The err. of Uncaught TypeError: Unsupported operand types: WC_Product_Simple + WC_Product_Simple.
This is because you are trying to add two product objects together using the += operator, which is not supported in PHP for objects.
you can try to collect the products in an array.
to retrieve products sold with a specific charity tag with a date range
use method ex
There are many mistakes in your code:
In your function code, you should check if the metadata
product_charity_tag
exist:Then your revised and commented code (where each product is added to an indexed array via the product ID, to avoid duplicates):