WordPress – Woocommerce – Changer le rôle d’un utilisateur lors de l’achat d’un produit – Change user role upon buying a product

<h2&>Changer le rôle d’un utilisateur à l’achat d’un produit spécifique woocommerce

Change a user role upon buying a specific product

functions.php
function lgbk_add_member( $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 == '8' ) {
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( 'membres2' );
}
}
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );

<h2&>Changer le rôle d’un utilisateur à l’achat d’un produit spécifique woocommerce parmis plusieurs produits

Change a user role upon buying a specific product amongst many products

functions.php
function lgbk_add_member( $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'];
// Set HERE your targetted products IDs:
$targetted_products = array( 9, 8 );
if ( $order->user_id > 0 )
if ( in_array( $product_id == $targetted_products ) ){
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( 'membres2' );
}
}
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );

Author: bejisan

Share This Post On