Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

Wordpress Woo-Commerce Query

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);

}

Open in new window

PHPWordPressSQL

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
PortletPaul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
And then just run a "foreach" or WHILE statement after it?
PortletPaul

If using php gather the information as you would from any multi row query ...

If you really want the logic done in sql please supply a mockup of what the values are individually and what you want them to be as a single row.
Your help has saved me hundreds of hours of internet surfing.
fblack61