Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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

}