How to change user rol after buying in Woocommerce - wordpress

thank you for any of you guys wanted to help me, I just want to know how to change the user role in WordPress when users buy something. I'm using WooCommerce. Regards

You can use Woocommerce action hooks. check my below code.
add_action( 'woocommerce_thankyou', 'change_user_role_afte_buying' );
function change_user_role_afte_buying( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '1', '2', '3' ); // your product ids for checking someting buy
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id ); // get user Object
$user->remove_role( 'customer' ); // remove old role
$user->add_role( 'new-role' ); // add new role
break; // Exit the loop
}
}
}

Related

Add extra customer note on order creation for specific user role in WooCommerce

For a specific user role I want to add a specific customer note to the order.
This is my code:
add_action( 'woocommerce_new_order', 'add_customer_note_user_role' );
function add_customer_note_user_role( $order_id ) {
$user_info = get_userdata(get_current_user_id());
if ( $user_info->roles[0]=="administrator" ) {
$order = wc_get_order( $order_id );
// The text for the note
$note = 'This is the message';
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
}
}
But this puts the message in the wrong place. When you check an order in the backend it's been displayed in the purple message boxes.
I want the message to be displayed as a customer note which is displayed under the shipping address. Because my API is picking up the notes from that place and puts them in our ERP.
I tried to change
$order->add_order_note( $note );
to
$order->add_order_note( $note, 'is_customer_note', true );
But without the desired result, any advice?
To display the message as a customer note displayed below the shipping address, you can use set_customer_note() instead.
So you get:
function action_woocommerce_new_order( $order_id ) {
// Get the WC_Order Object
$order = wc_get_order( $order_id );
// Get the WP_User Object
$user = $order->get_user();
// Check for "administrator" user roles only
if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
// The text for the note
$note = 'This is the message';
// Set note
$order->set_customer_note( $note );
// Save
$order->save();
}
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
To keep the original message (when NOT empty) use this instead:
function action_woocommerce_new_order( $order_id ) {
// Get the WC_Order Object
$order = wc_get_order( $order_id );
// Get the WP_User Object
$user = $order->get_user();
// Check for "administrator" user roles only
if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
// The text for the note
$note = 'This is the message';
// Get customer note
$customer_note = $order->get_customer_note();
// NOT empty
if ( ! empty ( $customer_note ) ) {
$note = $customer_note . ' | ' . $note;
}
// Set note
$order->set_customer_note( $note );
// Save
$order->save();
}
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );

Cancel all user orders on subscription cancelled/expired

I want to develop a setup/code in which all the orders made by the user gets cancelled when the his subscription gets cancelled or expired. I am using Wordpress, Woocommerce and Woocommerce Subscriptions plugin.
I am trying to do by using something like this:
function my_cancelled_subscription( $user_id, $subscription_key ) {
//what code should I use here????
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription', 10, 2 );
Please help mee.....
I figured it out..........
function my_cancelled_subscription( $subscription ) {
$order_id = $subscription->get_parent_id();
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
$order->update_status( 'cancelled' );
$args = array(
'customer_id' => $user_id,
'status' => 'wc-completed',
);
$arrorders = wc_get_orders( $args );
foreach ($arrorders as $ord) {
$ord->update_status( 'cancelled' );
}
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription' );

Woocommerce action `woocommerce_order_status_completed` not executing

We have a problem as I am not a woocommerce developer. The client wants to make a product out of stock once the order is completed (and payment received).
I added an action in functions.php
add_action( 'woocommerce_order_status_completed', 'set_product_out_of_stock', 10, 1 );
function set_product_out_of_stock( $order_id ) {
error_log( "This is a test: set product out of stock $order_id ^^", 0 );
//if ( 'completed' == $order_status) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$status = 'outofstock';
update_post_meta($item['item_id'], '_stock', 0);
update_post_meta( $item['item_id'], '_stock_status', wc_clean( $status ) );
wp_set_post_terms($item['item_id'], 'outofstock', 'product_visibility', true );
}
// }
}
The problem is that this action is not even triggered, The error_log doesn't show in the logs.. what may be the issue?
The website uses woocommerce v 4.7 .
Thank you !

Change role after purchasing a specific product on WooCommerce

I need to add a function which changes the user’s role after purchasing a specific product (by ID, or category). How can I do so?
You can use this code
add_action( 'woocommerce_order_status_completed', 'misha_change_role_on_purchase' );
function misha_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = 55; // that's a specific product ID
foreach ( $items as $item ) {
if( $product_id == $item['product_id'] && $order->user_id ) {
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'subscriber' );
}
}
}
If you want to check by category, just add a condition with has_term( $category_id, 'product_cat', $item['product_id'] )

Save WooCommerce Order Product Name to User Meta Custom Field

Caveat: I'm a solo freelance designer not a developer ;-)
I've created a custom field in the Wordpress user meta called membership.
I've tried the following code to save the WooCommerce Product Name to the membership custom field on checkout with help from this answer.
Updated attempt:
function wascc_woocommerce_checkout_update_user_meta_membership ( $customer_id, $posted ) {
if (isset($posted['$orderid'])) {
$order = $posted['$orderid'];
}
$theorder = new WC_Order( $order );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
update_user_meta( $customer_id, 'membership', $product_name);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'wascc_woocommerce_checkout_update_user_meta_membership', 10, 2 );
It produces no error, but does not save the product name to membership.
Help appreciated.
Last question may be related.
as I've commented out, you should use woocommerce_checkout_update_order_meta.
Something like this:
function wascc_woocommerce_checkout_update_user_meta_membership ( $order_id ) {
$theorder = new WC_Order( $order_id );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
// Gets the customer/user ID associated with the order. Guests are 0.
$customer_id = $theorder->get_user_id();
update_user_meta( $customer_id, 'membership', $product_name );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'wascc_woocommerce_checkout_update_user_meta_membership' );
I have doubts with your foreach loop though... you are looping just to get the last item?

Resources