Is there a better way to pull meta data than a query?

[ad_1]

I’m building a custom report for a client after a woocommerce order is placed on an event ticket sales website. Because the plugin stores all of the important info as meta data, I had to create a custom admin-new-order.php file that queries the database for the info I need to send to mailparser, ultimately to parse it and get it into Zapier where I can work with it.

The problem is that sometimes the ticket prices show up as $0, even when they weren’t complimentary tickets purchased with a coupon. I figured out that it’s happening because the WordPress database doesn’t have all the data in each area yet, or at least that’s what it seems like.

Adding a sleep(30) command at the top of the admin-new-order.php file has severely reduced the number of inaccuracies relating to the price, but they’re still showing up sometimes. The sleep timer is also manually set, and drastically slows down the movement from the “Checkout” screen to the “Order completed” screen for the end user.

I figured I would move the sleep command into it’s own if/while loop and check:

// This is shorthand, I know the syntax is not valid
if (the price == zero && a coupon != true) {

while (the price\[$g\] == 0) {
sleep(5);

price\[$g\] = <requery here>

}

};

I wanted to create a condition that checks to see if a coupon was used, but again, that queries the database and if the coupon meta data isn’t populated yet, I still wind up with the same issue.

So I started thinking “Maybe the queries are slowing the whole process way down” I mean, there’s ten queries done, then a loop that iterates through the data and assigns it to various arrays (23 of them) to keep the data in order. “Why ten queries but 23 arrays?” because I needed to create a table with consistent columns, and all the data had to match up for every single row. This was the only way I could think of to get it done.

All ten queries are short and sweet, like this one, which pulls the coupon (it’s actually the 11th query but I haven’t implemented it yet):

$coupon = $wpdb->get\_var(

$wpdb->prepare(

“SELECT order\_item\_name

FROM {$wpdb->prefix}woocommerce\_order\_items

WHERE order\_id = %d”,

$order\_id

)

);

&#x200B;

BUT, is there a more cpu-efficient way to get the data I need without performing a query? Or is this it? Because I don’t know how long it takes to populate the meta data for the coupon, and my if/while completely depends on it being there. 🙁

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer