Reputation: 31
Good Day,
I am fairly new to WordPress and WooCommerce. But I am fairly versed in PHP and MySQL. So my question is, how do I go about making a specific query for WooCommerce where I can point it to specific meta. For example, if I wanted to make a query to call all the different categories within WooCommerce> I would normally call it like this;
$args = array(
'number' => 'null',
'orderby' => 'name',
'order' => 'ASC',
'columns' => '4',
'hide_empty' => '1',
'parent' => '',
'ids' => ''
)
Ok so that would be my query, now what I am so struggling with is what do I do with it it now? Cause that part there is what everyone shows, no one tells you or shows you how to actually use the query or where to start. I have used the basic WP query function, but then the only categories I receive back come from WP and WC. So how do I point it directly to WC? Furthermore, these hooks annoy me so. So how do I go about doing it the normal way, bypassing WC and WP functions and build my own query;
$query = "SELECT * FROM wc_categories";
$result = mysql_query($query);
$temp_array = mysqli_fetch_array($result);
That is how I would love to do it, cause no one explains hooks and how to use it, or edit it, everyone assumes that you are pro-leet when it come to familial of WP workings.
So if anyone can point me in the right direction, I would really appreciate it. And I would love you long time if you could explain to me how to go about my 2nd code block cause that is my preferred way.
Kind Regards, DK
Upvotes: 3
Views: 1981
Reputation: 67
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");
Upvotes: 3