skip to Main Content

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


  1. 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.

    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
    }
    

    use method 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);
    
    Login or Signup to reply.
  2. There are many mistakes in your code:

    In your function code, you should check if the metadata product_charity_tag exist:

    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 = wc_get_product($item['product_id']);
    
            if ( $product->get_meta('product_charity_tag') ) {
                continue;
            }
            $post = get_post($item['product_id']);
            $product-> update_meta_data( 'product_charity_tag', get_the_author_meta('charity_tag', $$post->post_author ) );
            $product-> save();
        }
    }
    

    Then your revised and commented code (where each product is added to an indexed array via the product ID, to avoid duplicates):

    $orders = wc_get_orders( array(
        'type'        => 'shop_order',
        'limit'       => -1,
        'status'      => wc_get_is_paid_statuses(), // Paid order statuses (processing and completed)
        'date_before' => $last_day_month,
        'date_after'  => $first_day_month,
    ));
    
    if ( ! empty($orders) ) {
        $all_products = array(); // Initializing
    
        foreach ($orders as $order) {
            foreach ($order->get_items() as $item ) {
                $product_id = $item->get_product_id();
                $product    = wc_get_product( $product_id ); // Get the main product avoiding variations 
                
                // Check if the 'product_charity_tag' metadata exist
                if ( $product->get_meta('product_charity_tag') === 'bhf' ){
                    // Add the product to an indexed array by the product Id, avoiding duplicates
                    $all_products[$product_id] = $product; 
                }
            }
        }     
        print_r($all_products); 
    } else {
        print("No orders found");
    }    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search