根据在 Woocommerce 中购买的商品总数更改用户角色
Change user role based on total items purchased in Woocommerce
在 Woocommerce 中,我试图结合这两个功能:
获取用户订购商品的总量(所有时间)
function get_user_total_purchased_items( $user_id = 0 ){
global $wpdb;
$customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;
return (int) $wpdb->get_var( "
SELECT SUM(woim.meta_value)
FROM {$wpdb->prefix}woocommerce_order_items AS woi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
WHERE woi.order_item_type = 'line_item'
AND p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-completed')
AND pm.meta_key LIKE '_customer_user'
AND pm.meta_value LIKE '$customer_id'
AND woim.meta_key LIKE '_qty'
" );
}
如果用户订购了某个商品,第二个会更改用户角色。
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 == '416' ) {
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( 'premium' );
}
}
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
我想要完成的是,如果用户总共订购了 50-99 件商品,他的用户角色将从 standard_user 更改为 bronze_user,如果他订购了 100-199 件他的用户角色 bronze_user -> silver_user 如果项目总数超过 200 hus 用户角色 silver_user -> gold_user.
第一个函数运行良好,来自 。
现在只是想弄清楚如何将它实现到第二个函数中。
从 Woocommerce 3 开始,您的代码有点过时,需要进行一些更改。以下应根据购买商品的数量有条件地更改用户角色 (未测试):
function get_user_total_purchased_items( $user_id = 0 ){
global $wpdb;
$customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;
return (int) $wpdb->get_var( "
SELECT SUM(woim.meta_value)
FROM {$wpdb->prefix}woocommerce_order_items AS woi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
WHERE woi.order_item_type = 'line_item'
AND p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-completed')
AND pm.meta_key LIKE '_customer_user'
AND pm.meta_value LIKE '$customer_id'
AND woim.meta_key LIKE '_qty'
" );
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 20, 2 );
function change_role_on_purchase( $order_id, $order ) {
$user_id = $order->get_customer_id();
$user = new WP_User( $user_id );
$purchases = get_user_total_purchased_items( $user_id );
if( $purchases >= 50 && $purchases < 100 && ! in_array( 'bronze_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'bronze_user' );
}
elseif( $purchases >= 100 && $purchases < 200 && ! in_array( 'silver_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'bronze_user' );
// Add role
$user->add_role( 'silver_user' );
}
elseif( $purchases >= 200 && ! in_array( 'gold_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'silver_user' );
// Add role
$user->add_role( 'gold_user' );
}
if ( $user_id > 0 && $product_id == '416' ) {
foreach ( $order->get_items() as $item ) {
if ( $order->user_id > 0 && $item->get_product_id() == '416' ) {
update_user_meta( $user_id, 'paying_customer', 1 );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'premium' );
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。我希望它会起作用,因为我无法测试它。
有关 Woocommerce 3 中订单的参考:
在 Woocommerce 中,我试图结合这两个功能:
function get_user_total_purchased_items( $user_id = 0 ){
global $wpdb;
$customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;
return (int) $wpdb->get_var( "
SELECT SUM(woim.meta_value)
FROM {$wpdb->prefix}woocommerce_order_items AS woi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
WHERE woi.order_item_type = 'line_item'
AND p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-completed')
AND pm.meta_key LIKE '_customer_user'
AND pm.meta_value LIKE '$customer_id'
AND woim.meta_key LIKE '_qty'
" );
}
如果用户订购了某个商品,第二个会更改用户角色。
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 == '416' ) {
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( 'premium' );
}
}
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
我想要完成的是,如果用户总共订购了 50-99 件商品,他的用户角色将从 standard_user 更改为 bronze_user,如果他订购了 100-199 件他的用户角色 bronze_user -> silver_user 如果项目总数超过 200 hus 用户角色 silver_user -> gold_user.
第一个函数运行良好,来自
现在只是想弄清楚如何将它实现到第二个函数中。
从 Woocommerce 3 开始,您的代码有点过时,需要进行一些更改。以下应根据购买商品的数量有条件地更改用户角色 (未测试):
function get_user_total_purchased_items( $user_id = 0 ){
global $wpdb;
$customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;
return (int) $wpdb->get_var( "
SELECT SUM(woim.meta_value)
FROM {$wpdb->prefix}woocommerce_order_items AS woi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
WHERE woi.order_item_type = 'line_item'
AND p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-completed')
AND pm.meta_key LIKE '_customer_user'
AND pm.meta_value LIKE '$customer_id'
AND woim.meta_key LIKE '_qty'
" );
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 20, 2 );
function change_role_on_purchase( $order_id, $order ) {
$user_id = $order->get_customer_id();
$user = new WP_User( $user_id );
$purchases = get_user_total_purchased_items( $user_id );
if( $purchases >= 50 && $purchases < 100 && ! in_array( 'bronze_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'bronze_user' );
}
elseif( $purchases >= 100 && $purchases < 200 && ! in_array( 'silver_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'bronze_user' );
// Add role
$user->add_role( 'silver_user' );
}
elseif( $purchases >= 200 && ! in_array( 'gold_user', $user->roles ) ) {
// Remove role
$user->remove_role( 'silver_user' );
// Add role
$user->add_role( 'gold_user' );
}
if ( $user_id > 0 && $product_id == '416' ) {
foreach ( $order->get_items() as $item ) {
if ( $order->user_id > 0 && $item->get_product_id() == '416' ) {
update_user_meta( $user_id, 'paying_customer', 1 );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'premium' );
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。我希望它会起作用,因为我无法测试它。
有关 Woocommerce 3 中订单的参考: