Leave a Reply

1 comment

  1. Creating query using standard wp/wc function is easy. For example if you want to get customer details for specific orders. Required codes might look like this (notice the global function):

    global $post, $woocommerce, $the_order;
    if ( empty( $the_order ) || $the_order->id != $post->ID )
        $the_order = new WC_Order( $post->ID );
    
    $user_info = get_userdata( $the_order->user_id );
    $biling_email = $the_order->billing_email;
    $phone_no = $the_order->billing_phone;
    

    If you want to query a custom order meta field, you may use a code something like below. You need to know the custom field name in database:

    $custom_field = get_post_meta( $post->ID, 'custom_field', true );
    

    For custom query you need to initialize global wpdb function first by adding global $wpdb function before your query. Even this will work with a WP page only, it’ll not work with a page created outside WP. Below is an example:

    global $wpdb
    
    $table = $wpdb->prefix . 'postmeta'; //Good practice
    $orderno = $wpdb->get_results( "SELECT * FROM $table");