I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID
Here is what I'm doing to test if I can get the customer ID:
function get_customeruserid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$customer = new WC_Customer($post->ID);
$user_id = $customer->get_ID();
$value = $user_id;
return $value;
}
This returns a 0.
However, I can get the order number easily enough by doing this:
function get_customerorderid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$value = $order_id;
return $value;
}
This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.
Any ideas? I'm a newbie at php and probably very bad.
You could use the order number to get the corresponding customer id using a database query, google wordpress meta query
For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used. Thank you for your help getting it solved.
While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:
Then make sure to add the "all_export_mycred" to the php return field.
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
In WooCommerce 2.5 to 3.0+ you can use
get_post_meta()
function this way:In WooCommerce 3.0+ you can use the class WC_Order methods this way: