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.
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
In WooCommerce 3.0+ you can use WC_Order Class methods this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
} else {
$order_id = $order->id;
}
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
return $user_id;
}
Before WooCommerce 3.0 version, you can use get_post_meta() function this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
} else {
$order_id = $order->id;
}
// Get the user ID
$user_id = get_post_meta($order_id, '_customer_user', true);
return $user_id;
}
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:
function all_export_mycred($balance)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$user_id = $order->get_user_id( );
$balance = mycred_get_users_balance( $user_id );
return $balance;
}
Then make sure to add the "all_export_mycred" to the php return field.
Related
i'm having a odd issue with WooCommerce.
I have a plugin I developed that sends data from WooCommerce orders to HubSpot. The plugin needs to fire whenever a new order is placed OR an existing order is updated . I've been using the following truncated code to try to achieve this:
add_action('save_post', 'printout', 10, 3);
function printout($post_ID, $post, $update)
{
if ("shop_order" == $post->post_type) {
$msg = $post_ID;
delegator($msg);
}
}
function delegator($order_id){
$order = get_woocommerce_order($order_id);
// assigns the actual data of the order to the $data variable
$data = get_woocommerce_order_data($order);
// assign User e-mail for search
$email = $order->get_billing_email();
//$email = get_post_meta( $order_id, '_billing_email', true );
}
The problem i'm having is that upon new Orders being made, WordPress can't get the order details, specifically a e-mail (it returns null). The hook does return an order number, so I don't think it's the hook doing this but something else.
The odd part is if after the order is placed and I go into the Order page and just hit update on the order, it works!
So I'm wondering what I'm missing / what's wrong here. Does the flow of the hook fire before WooCommerce can make database calls?
Thanks for any help. Been driving me mad!
For orders added/updated through admin, use the code below
add_action('save_post_shop_order', 'backend_delegator', 10, 3);
function backend_delegator($post_id, $post, $update) {
// Orders in backend only
if (!is_admin())
return;
// Get an instance of the WC_Order
$order = new WC_Order($post_id);
//Fired when create a new order
if (!$update) {
$email = $order->get_billing_email();
}
if ($update) {
$email = $order->get_billing_email();
}
}
For orders placed from checkout, use the code below.
add_action('woocommerce_new_order', 'frondend_delegator', 10, 2);
function frondend_delegator($order_id, $order) {
$email = $order->get_billing_email();
}
I'm trying to make a function that get the count of product reviews in WooCommerce product page.
I need to use it in another function in a logical operation ... Can't figure out whats wrong.
function reviews_count() {
$id = $product->get_id();
$product = wc_get_product($id);
$count = $product->get_review_count();
return $count;
}
Try the following instead (for single product_pages):
function reviews_count() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
return $product->get_review_count();
}
Or you can also add the product Id as function argument (to use it in another function) like:
function reviews_count( $product_id ) {
$product = wc_get_product( $product_id );
return $product->get_review_count();
}
So in your other function, you will be able to pass the product Id a bit like:
function my_other_function() {
global $product;
$count = reviews_count( $product->get_id() );
}
When customer click cancel subscription, they still can logged in the my account section, but I want to let user cannot access anything on my account section. From this point, I would like to make the function on "if the user cancels payment, it also delete the account too" to make them cannot access, so I try to investigate and write the code below.
add_action( 'woocommerce_order_status_cancelled',
'custom_woocommerce_auto_delete_user' );
function custom_woocommerce_auto_delete_user( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ( !$order_id )
return false;
if ('cancelled' == $order_status) {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
However, I'm just the beginner of woocommerce and I don't know which solution is the best for this issue. I'm so glad for anyone that come to response to me :)
I want to delete the users who are created when trying to buy a subscription but the payment gets failed although the user anyhow creates. I am just to confirm if this code works for Me or not?
add_action('woocommerce_subscription_status_failed', 'custom_woocommerce_subscription_status_failed');
function custom_woocommerce_subscription_status_failed($order_id)
{
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ($order_status`enter code here` == 'failed') {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
I'm trying to get price without currency in a function I made.
function add_price_widget()
{
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price_html();
echo thePrice;
}
Displays: 100kr
How do I get it to just give me the price 100
What #Syntax_Error had said is correct you have to use
get_price(), WooCOmmerce also provide a wrapper function
wc_get_product() for WC_Product class.
So your function would look something like this:
function add_price_widget()
{
$product = wc_get_product(get_the_ID());
$thePrice = $product->get_price(); //will give raw price
echo $thePrice;
}
Hope this helps!
You can just use the function get_price that returns only the number (without dots or symbol)
function add_price_widget() {
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price();
echo thePrice;
}
I just tested it in my site and it work. So it should work for you too.
I need to add custom args to PayPal paying link.
The PHP function looks like this:
$paypal_args = apply_filters( 'woocommerce_paypal_args', $paypal_args );
add_filter( 'woocommerce_paypal_args' , 'change_paypal_args' );
function change_paypal_args( $paypal_args ) {
global $wp;
global $woocommerce;
$order = wc_get_order( $order_id );
$paypal_args['invoice'] = 'spi432';
$paypal_args['txn_type'] = 'cart';
$paypal_args['payment_date'] = $order->order_date;
return $paypal_args;
}
I added txn_type and invoice as arguments to the link. But payment_date is not shown.
What may be the problem? Also, how can I display email of the customer?
If you enabled WP_DEBUG you would probably see that $order_id is undefined, and therefore $order is not an order object, so $order->order_date is likely a fatal error. Try passing the order as the second parameter instead.
add_filter( 'woocommerce_paypal_args' , 'so_42424283_change_paypal_args', 10, 2 );
function so_42424283_change_paypal_args( $paypal_args, $order ) {
$paypal_args['invoice'] = 'spi432';
$paypal_args['txn_type'] = 'cart';
// WC 2.6+
$paypal_args['payment_date'] = $order->order_date;
// WC 2.7
//$paypal_args['payment_date'] = $order->get_date_created();
return $paypal_args;
}