admin管理员组文章数量:1390204
I am trying to create a function to add a new user role to any customer who purchases a specific item via Woocommerce.
I have the following code which achieves this with a paid product, however, the product that triggers this is a free item. I am not sure if the code "paying_customer" is stopping this function from working with a free item order.
Is there an alternative to using "paying_customer" or a better way of achieving this?
Below is the code that works for paid products, but not for free products.
Thanks
function change_role_on_purchase( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
if ( $order->user_id > 0 && $product_id == '13874904' ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'special_subscriber' );
}
}
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
I am trying to create a function to add a new user role to any customer who purchases a specific item via Woocommerce.
I have the following code which achieves this with a paid product, however, the product that triggers this is a free item. I am not sure if the code "paying_customer" is stopping this function from working with a free item order.
Is there an alternative to using "paying_customer" or a better way of achieving this?
Below is the code that works for paid products, but not for free products.
Thanks
function change_role_on_purchase( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
if ( $order->user_id > 0 && $product_id == '13874904' ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'special_subscriber' );
}
}
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
Share
Improve this question
edited Mar 14 at 17:48
LoicTheAztec
255k24 gold badges399 silver badges446 bronze badges
asked Mar 14 at 11:43
RichRBXRichRBX
351 silver badge4 bronze badges
1
|
3 Answers
Reset to default 1Your code is obsolete, can be simplified and has some mistakes like:
- You can get the WC_Order object directly as 2nd argument from the function,
- Since WooCommerce 3, you need to use methods on the WC_Order object, instead of properties,
- You should check orders with processing and completed status...
Try the following:
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 10, 2 );
add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase', 10, 2 );
function change_role_on_purchase( $order_id, $order ) {
$user = $order->get_user(); // Get the WP_User Object
// Loop through order line items
foreach ( $order->get_items() as $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
if ( $user && in_array('13874904', [$product_id , $variation_id]) ) {
// Set user as paying customer if not set yet
wc_paying_customer( $order_id );
// Check for "subscriber" user role and replace it
if ( wc_user_has_role( $user, 'subscriber' ) ) {
$user->set_role( 'special_subscriber' );
}
break; // Stop the loop
}
}
}
It should better work.
Related:
- How to get WooCommerce order details
- Get Order items and WC_Order_Item_Product in WooCommerce 3
- How can I get customer details from an order in WooCommerce?
From WooCommerce Docs:
Orders containing only products that are both virtual and downloadable will skip the Processing order status and move directly to the Completed order status.
So if your free product is either virtual or downloadable and if your order doesn't contain any other paid item then the hook woocommerce_order_status_processing
will not be fired. Instead use the hook woocommerce_order_status_completed
.
To ensure your function runs for free orders, use the woocommerce_order_status_completed
hook, as free orders are most likely set to completed. Also, it is a good idea to check if the order is paid or not, and if not, run the role change.
function change_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id ); // Use wc_get_order for better compatibility
if (!$order) return; // If order is not valid, exit the function.
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( $order->get_user_id() > 0 && $product_id == '13874904' ) {
$user_id = $order->get_user_id();
update_user_meta( $user_id, 'special_subscriber_product_purchased', 1 ); //Use a more descriptive meta key.
$user = new WP_User( $user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'special_subscriber' );
break; // No need to continue looping if the specific product is found.
}
}
}
add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase' );
function change_role_on_purchase_pending( $order_id ) {
$order = wc_get_order( $order_id ); // Use wc_get_order for better compatibility
if (!$order) return; // If order is not valid, exit the function.
if ($order->get_total() > 0) return; // If order is paid, exit.
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( $order->get_user_id() > 0 && $product_id == '13874904' ) {
$user_id = $order->get_user_id();
update_user_meta( $user_id, 'special_subscriber_product_purchased', 1 ); //Use a more descriptive meta key.
$user = new WP_User( $user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'special_subscriber' );
break; // No need to continue looping if the specific product is found.
}
}
}
add_action( 'woocommerce_order_status_pending', 'change_role_on_purchase_pending' );
本文标签: phpHow to update user role after woocommerce order for a free itemStack Overflow
版权声明:本文标题:php - How to update user role after woocommerce order for a free item - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744658798a2618118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$product_id == '13874904'
the free or paid product? Your code only checks for 1 product ID. – Richard Commented Mar 14 at 12:33