You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce订单完成时如何根据产品ID及购买数量分配用户角色?

Solution for Quantity-Based User Role Assignment in WooCommerce

Hey there! Let's get that quantity-based role assignment working for your WooCommerce store. Your existing code already handles role assignment by product ID—we just need to extend it to track item quantities and apply the extra role when the threshold is hit.

Step-by-Step Modifications

Here's how to update your code to meet all your requirements:

  • Track quantities for specific items: We'll add a variable to count how many of Item-B the user purchased.
  • Combine logic into a single loop: Instead of looping through order items multiple times, we'll handle all checks in one go for better efficiency.
  • Add the extra role when quantity meets the threshold: After counting, we'll check if the quantity of Item-B is 2 or more, then assign the additional role.

Updated Code

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );
function wpglorify_change_role_on_purchase( $order_id ) {
    // Get the order object (use wc_get_order for newer WooCommerce versions)
    $order = wc_get_order( $order_id );
    if ( ! $order ) return;

    $items = $order->get_items();
    $user_id = $order->get_user_id();
    
    // Exit if no user is attached to the order
    if ( ! $user_id ) return;

    // Define your product IDs and corresponding roles
    $item_a_id = 50; // Replace with your actual Item-A ID
    $item_b_id = 43; // Replace with your actual Item-B ID
    $role_a = 'user-role-a';
    $role_b = 'user-role-b';
    $role_c = 'user-role-c';

    // Track total quantity of Item-B in the order
    $item_b_total_quantity = 0;

    // Loop through order items once to handle all checks
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        $quantity = $item->get_quantity();

        // Assign role for Item-A purchase
        if ( $product_id === $item_a_id ) {
            $user = new WP_User( $user_id );
            $user->add_role( $role_a );
        }

        // Assign role for Item-B purchase and accumulate quantity
        if ( $product_id === $item_b_id ) {
            $user = new WP_User( $user_id );
            $user->add_role( $role_b );
            $item_b_total_quantity += $quantity;
        }
    }

    // Assign extra role if total Item-B quantity is 2 or more
    if ( $item_b_total_quantity >= 2 ) {
        $user = new WP_User( $user_id );
        $user->add_role( $role_c );
    }
}

Key Notes About the Updated Code

  • wc_get_order compatibility: This is the recommended way to retrieve orders in modern WooCommerce versions, replacing the older new WC_Order approach to avoid deprecation issues.
  • Single loop efficiency: Processing all items in one iteration cuts down on redundant work and keeps the code clean.
  • Accurate quantity tracking: We sum up the total quantity of Item-B across all instances in the order (even if the user added it multiple times to their cart).
  • Multi-role support: WordPress's add_role method allows users to hold multiple roles, so all assigned roles will remain active on the user's account.

Testing Tips

  • Replace the placeholder product IDs (50, 43) and role names (user-role-a, etc.) with your actual store values.
  • Test by placing an order with 2+ units of Item-B, set the order status to "Completed", then check the user's roles in the WordPress admin to confirm all roles are applied correctly.

内容的提问来源于stack exchange,提问作者kmish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 15:08:11