根据在Woocommerce中购买的商品总数更改用户角色

时间:2018-08-04 10:40:11

标签: php wordpress woocommerce orders user-roles

在Woocommerce中,我试图结合这两个功能:

first one获取用户订购的商品总数(一直)

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更改为铜牌用户,如果他订购了100-199,则他的用户角色铜牌用户-> silver_user,如果总计项目超过200个Hus用户角色silver_user-> gold_user。

第一个功能运行良好,它来自this answer

现在只想弄清楚如何在第二个函数中实现它。

1 个答案:

答案 0 :(得分:3)

自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中订单的参考,