troubleshooting Question

Wordpress Woo-Commerce Query

Avatar of Robert Granlund
Robert GranlundFlag for United States of America asked on
PHPWordPressSQL
3 Comments1 Solution139 ViewsLast Modified:
I am using Woo-Commerce with Wordpress.  I am trying to write a query that pulls specific Meta_Key Values but I'm not 100% on how to write it.

I need the order ID to locate all of the meta data, that is no problem, then I need to query those fields with that ID and pull specific values from meta_keys.  The query below is just a start but will get me going so I can ask additional questions.
Right now I have four meta_key that I need to pull the value from;
1.First Name (meta_key value / First Name)
2. Middle Name (meta_key value / Middle Name)
3. Last Name (meta_key value / Last Name)
4. Company Name (meta_key value / Company Name)

<?php
 global $wpdb;
        $order_id = $_POST['order_number'];
  
        
         $order = $wpdb->get_results ( "SELECT order_item_id, order_item_name FROM woocommerce_order_items WHERE order_id = '$order_id'");

         foreach ($order as $page) {
            echo $page->order_item_id.'<br/>'.$page->order_item_name;
            $meta_id = $page->order_item_id;
            $entity = $page->order_item_name;

    }
        
     
$sql = $wpdb->get_results ( "SELECT
    CASE WHEN $entity = 'ABC' 
    THEN CONCAT(meta_key ='First Name', IF (CHAR_LENGTH(meta_key = 'Middle Name') > 0, CONCAT(meta_key = 'Middle Name')), meta_key ='Last Name')
    
    WHEN $entity = DEF' 
    THEN CONCAT(meta_key ='First Name', IF (CHAR_LENGTH(meta_key = 'Middle Name') > 0, CONCAT(meta_key = 'Middle Name')), meta_key ='Last Name')
    
ELSE meta_key ='LLCName'
    END 
    AS 'f1_02(0)'
    FROM
    woocommerce_order_itemmeta
    WHERE order_item_id = '$meta_id'");

       foreach ( $sql as $page )
{
   echo $page->f1_02(0);

}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros