On Woocommerce order complete, activate woosensei course - wordpress

I'm developing a plugin with Woocommerce and Woosensei
I can't seem to get the process working of creating an order programmatically, mark the order completed and activate the woosensei course for that particular person.
// create a new checkout instance and order id
$checkout = new WC_Checkout();
$this_order_id = $checkout->create_order();
// add some data to the order here //
[ ......... ]
// execute order
$order = new WC_Order($this_order_id);
$order->update_status('completed');
$learning = new WooThemes_Sensei();
$learning->sensei_woocommerce_complete_order($this_order_id);
$cart = new WC_Cart();
$cart->empty_cart();
The order is marked as complete indeed, but the user doesn't have access to the course.
If I manually change the order back to in processing and after that to completed (in the WP admin), then the course is assigned to the user.
This is really annoying, hope somebody has some cues (Woo is poorly documented ....)
Thx in advance!

Hmm, it seems like the sensei_woocommerce_complete_order function checks whether a user is logged in or not.
So I duplicated that function and removed the 'logged in' check, and called that specific function when the user is not logged in.
End to the grey hairs ... ;-)

Related

Add WooCommerce coupon through URL

I'm having troubles with applying coupons. The code below work except when the user visits the site for the first time and nothing has been added to the cart yet. I need to clear my cache in order to reproduce this error.
function process_url()
{
if( empty($_GET['coupon']) )
return;
// Add the discount.
global $woocommerce;
$result = $woocommerce->cart->add_discount($_GET['coupon']);
}
add_action('init', 'process_url');
Am I doing something wrong?
Apparently the user session is not stored if you apply a coupon. You need to have first added a product or do something else which stores your session. We can do it manually though, namely by creating the session cookie ourselves.
if( !WC()->session->has_session() )
WC()->session->set_customer_session_cookie(true);
Took me all day. Hopefully someone will be able use it.

woocommerce order to mark as completed upon visiting a specific page

I want a woocommerce order to be automatically marked as completed upon visiting a specific page in WordPress
This specific page will have the order_id in GET variable. Now what i need to know is how to find an order by order_id and mark the status as completed on a specific WordPress page.
You can do it with following piece of code:
if(is_page('page_title')){
$order = new WC_Order($_GET['your_order_id']);
//wc-completed, wc-processing
$update_status = array('ID'=>$_GET['your_order_id'],'post_status'=>'wc-completed');
wp_update_post( $update_status );
}
First check whether It is on your specific page or not.
Second thing is to get your order
Third step is to update status of order.
Let me know if you have any doubt.
EDITED
You can refer codex for more details on is_page.
If you have page_id then you need to put condition like if(is_page(42)) where 42 is ID of your page.
So, Rohil_PHPBeginner's answer works like a charme. If you urgently search for a "just copy and paste" solution and have no time to figure things out, go ahead and use this ready to use snippet for your functions.php:
Don't forget to replace the number 42 with the page id of your desired page that will handle as an order-completion-url. Afterwards you can call the order completion by url by: http://your-url.com/yourdeletionpage/?your_order_id=ORDER_ID_TO_BE_COMPLETED
function my_abhaken() {
if(is_page(42)){
$order = new WC_Order($_GET['your_order_id']);
//wc-completed, wc-processing
$update_status = array('ID'=>$_GET['your_order_id'],'post_status'=>'wc-completed');
wp_update_post( $update_status );
}
}
add_action('wp_head', 'my_abhaken');

what is the meaning of 1 in user status field of wp_users in wordpress CMS?

We are using wordpress for your website development. User is active when user_status=2 and user is inactive if user_status=0. Then what is the meaning of user_status=1.
Please provide your valuable suggestions.
https://wordpress.org/support/topic/what-is-the-status-of-user_status
The user_status field is effectively a dead record in the database.
It's been that way for some time.
You could certainly make use of it for your own purpose, but as it is
a sort of deprecated or unusued element, it's always possible it will
be dropped from a future version of WordPress. Or even be put back to
work.
Unfortunately, WordPress doesn't provide native online/offline user status methods. You'll have to implement it by yourself.
Some ideas how to implement it right, could be found in that topic:
https://wordpress.stackexchange.com/q/34429/44533
Another option is to use some 3rd-party plugin ( I can't advice any...).
In my own solution, I'm creating user_login custom filed in wp_usermeta table, to check user status.
//Creating hooks for login/logout actions:
add_action('clear_auth_cookie', array('WP_Plugin_Template','set_user_logged_out'), 10);
add_action('wp_login', array('WP_Plugin_Template','set_user_logged_in'), 10, 2);
//When hook is triggered, I'm using user_meta to update user status:
function set_user_logged_in($user_login, $user) {
if(get_user_meta($user->ID, "logged_in", true) !== "true")
if(!update_user_meta($user->ID, 'logged_in', 'true'))
wp_die("Failed to add usermeta ", "Fatal");
}
function set_user_logged_out() {
$user = wp_get_current_user();
if(get_user_meta($user->ID, "logged_in", true) !== "false")
if(!update_user_meta($user->ID, 'logged_in', 'false'))
wp_die("Failed to add usermeta ", "Fatal");
}
Hope it helps.
From other threads I see that user_status is effectively a "dead" field. It remains in the wp_user table, but isn't used by WP itself for anything anymore. Probably explains why wp_update_user doesn't touch it.
global $wpdb;
$wpdb->query('UPDATE wp_users SET user_status = 1 WHERE ID = '.$current_user->ID);
FOR MORE
http://codex.wordpress.org/Class_Reference/wpdb
I might be late here but the user_status is not dead as it sounds, it is used mosty in network/multisite to mark user as spam ;-)
You should probably use add_user_meta (WP Codex) and add a new field to your users table.
Seems like the cleanest way to me and you won´t be surprised, if user_status gets dropped from the database some time in the future.
Here are what each user_status means:
user_status = 0 => false or normal status
user_status = 1 => User marked as spammer
user_status = 2 => User pending (user account not activated yet)
The user_status is not dead as it sounds, it is used mostly in
network/multisite to mark the user as spam or ham.
This field is mostly used in multisite
https://developer.wordpress.org/reference/functions/update_user_status/

Get WooCommerce Subscription Original (Initial) order number

I am working on a project where a user would purchase a subscription and when they do we send data to another server using API calls. Once the server receives the necessary information it creates a serial number and sends it back to the woocommerce site.
This all works just fine, I am successful in sending data and retrieving serial numbers and synchronizing most things on the server.
I am stuck at when Woo Subscriptions renews their order I need to update information on the other server regarding the serial number. I think I would be fine if I could get access to the original order number.
The other issue I am running into is determining if the order is in fact a renewal order, I have a trivial flag set up that checks if "subscription_interval" is inside of the order->get_items, if not then its a renewal order. Something is just fishy about this whole thing.
Basically I need a way to find out if it is a renewal order and if it is give me the initial order number.
Looking at the order screen on the very bottom of the metaboxes (advanced) there is a metabox that shows "Related Subscription Orders" and even shows the initial order. How can I access this data?
Thanks in advance!
Ok, so I found WC_Subscriptions_Renewal_Order and ran a get_class_methods on it. I found is_renewal and get_parent_order_id, life is good again :)
Maybe this can help someone else looking for a way to find out the original order ID by the subscription ID.
function my_get_original_order_id_by_subscription_id( $sub_id ){
$sub_post = get_post( $sub_id );
if( $sub_post && is_object( $sub_post ) && isset( $sub_post->post_parent ) && absint( $sub_post->post_parent ) > 0 ){
return $sub_post->post_parent;
}
return false;
}
The solution is very simple. You need just to get the object post related to the subscription ID ($sub_id), and then the post_parent is the ID of the original order that you were looking for.

Users last login date - Drupal

In drupal how to display the user's last login date and Time.I tried out the code
user->login
It displays the current login time, But I want users previous login time and date.
Take a look at the User Stats module, it might be something that could work for you. From the module's project page:
Provides commonly requested user statistics for themers, IP address tracking and Views
integration. Statistics are:
Days registered
Join date
Days since last login
Days since last post
Post count
Login count
User online/offline
IP address
These data are saved with module Login History
Drupal doesn't offer this natively. If you need to use it, you would probably want to add it to for example serialized $user->data array when user logs in (Using hook_user() for $op = "login") and save updated user object afterwards, and then you will be able to fetch it on the next login.
The solution I used: keep track of the last two log-in timestamps (the current and the previous one).
/**
* Implementation of hook_user()
*/
function your_module_user($op, &$edit, &$account, $category = NULL) {
switch($op) {
// Successful login
case 'load':
// If it's less than two it means we don't have a previous login timtestamp yet.
$account->custom_last_login = sizeof($account->custom_login_history) < 2
? NULL
: array_pop($account->custom_login_history);
break;
case 'login':
// If it's the first time, we don't have a history
$login_history = is_array($account->custom_login_history)
? $account->custom_login_history
: array();
// Get rid of the old value.
if (sizeof($login_history) == 2) {
array_pop($login_history);
}
// Add to the history the current login timestamp.
array_unshift($login_history, $account->login);
user_save($account, array('custom_login_history' => $login_history));
break;
}
}
Then in your template you just use $user->custom_last_login. If it's empty it means we don't have the previous timestamp yet, will be available after the next login.

Resources