Home > Software design >  Save post meta data (result of a SQL query) when using the "woocommerce_thankyou" hook
Save post meta data (result of a SQL query) when using the "woocommerce_thankyou" hook

Time:05-19

I'm attempting to query my database and pass the result into the update_post_meta function. However not sure if I'm constructing this correcltly or whether there's an issue with my $order_id usage?

I need the post meta to update with the query results for the current logged in user and current order once an order is made, so thought the woocommerce_thankyou hook made sense to use however upon completing an order no post meta is written.

add_filter( 'woocommerce_thankyou', 'my_function', 10, 2);
function my_function( $result, $order_id ) {
    // Load the global $post
    global $woocommerce, $post;

    // Get the post ID
    $order_id = $post->ID;

    // Then you can get the order object
    $order = wc_get_order( $order_id );


    $user_ID = get_current_user_id();

    //SQL
    global $wpdb;

    return $wpdb->get_var("SELECT SUM(b03_woocommerce_order_itemmeta.meta_value)
    FROM b03_woocommerce_order_itemmeta
    JOIN b03_woocommerce_order_items ON b03_woocommerce_order_itemmeta.order_item_id = b03_woocommerce_order_items.order_item_id
    JOIN b03_posts ON b03_woocommerce_order_items.order_id = b03_posts.ID
    JOIN b03_postmeta ON b03_posts.ID = b03_postmeta.post_id
    WHERE b03_posts.post_type = 'shop_order'
    AND b03_woocommerce_order_itemmeta.meta_key = 'trees_planted'
    AND b03_postmeta.meta_value = $user_ID
    AND b03_postmeta.meta_key = '_customer_user'
    AND b03_posts.ID = $order_id");
    
    update_post_meta( $order_id, 'trees',$wpdb);
}

Appreciate any advice on how best to handle this?

CodePudding user response:

Your code attempt contains multiple errors and mistakes:

  • woocommerce_thankyou is an action hook, not a filter hook
  • Only the $order_id is passed to the callback function, $result is not applicable
  • Use $wpdb->prefix vs b03_, this to make it dynamic
  • $wpdb is an object
  • The use of global $woocommerce, $post; is redundant

So you get:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get user id
        $user_id = $order->get_user_id();
        
        // Not a guest
        if ( $user_id > 0 ) {
            //SQL
            global $wpdb;

            // The SQL query
            $result = $wpdb->get_var( "
                SELECT SUM( oim.meta_value )
                FROM {$wpdb->prefix}woocommerce_order_itemmeta as oim
                JOIN {$wpdb->prefix}woocommerce_order_items as oi ON oim.order_item_id = oi.order_item_id
                JOIN {$wpdb->prefix}posts as p ON oi.order_id = p.ID
                JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
                WHERE p.post_type = 'shop_order'
                AND oim.meta_key = 'trees_planted'
                AND pm.meta_value = '$user_id'
                AND pm.meta_key = '_customer_user'
                AND p.ID = '$order_id'
            " );

            // REMOVE THIS, only for testing purposes
            $result = 10;

            // Add the meta data
            $order->update_meta_data( 'trees', $result );
            $order->save();
        }
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

Note: because you're using a custom SQL query, of which the data/result does not exist in general/default in WooCommerce, but only for you specifically, I have replaced it in my answer with a fixed value of 10. Adjust where necessary!

  • Related