WooCommerce 订单状态完成时根据产品 ID 和数量添加用户角色

发布于 2025-01-13 05:30:06 字数 1414 浏览 0 评论 0原文

我尝试根据几个不同的标准分配用户角色:

  • 如果用户购买 Item-A,则分配 user-role-a。
  • 如果用户购买了 Item-B,则分配 user-role-b。
  • 如果用户购买了 2 个或更多 Item-B,则还会分配 user-role-c。

因此可以分配多个用户角色。基于购买产品后更改用户角色,这是我的尝试:

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );

function wpglorify_change_role_on_purchase( $order_id ) {

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

// CHILD PRODUCT MAKES PRIMARY ADULT ROLE
    
    $product_id = 50; // that's the child product ID

    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

            // Add new role
            $user->add_role( 'primary-adult' );
        }
    }

// ADULT PRODUCT MAKES ADULT ROLE
    
    $product_id = 43; // that's the adult product ID
    
    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

            // Add new role
            $user->add_role( 'adult' );
        }
    }   
    
}

通过我的代码,我可以获得一些解决方案(根据购买的产品 ID 分配角色),但我仍然需要能够根据购买的商品的数量分配角色。有什么建议吗?

I'm trying to assign user roles based on a few different criteria:

  • If a user purchases Item-A, user-role-a is assigned.
  • If a user purchases Item-B, user-role-b is assigned.
  • If a user purchases 2 or more of Item-B, user-role-c is also assigned.

So multiple user roles can be assigned. Based on Change User Role After Purchasing Product, this is my attempt:

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );

function wpglorify_change_role_on_purchase( $order_id ) {

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

// CHILD PRODUCT MAKES PRIMARY ADULT ROLE
    
    $product_id = 50; // that's the child product ID

    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

            // Add new role
            $user->add_role( 'primary-adult' );
        }
    }

// ADULT PRODUCT MAKES ADULT ROLE
    
    $product_id = 43; // that's the adult product ID
    
    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

            // Add new role
            $user->add_role( 'adult' );
        }
    }   
    
}

With my code I was able to get a bit of a solution (assign roles based on purchased product IDs), but I still need to be able to assign a role based on the quantity of a purchased item. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2025-01-20 05:30:06

除了回答你的问题之外,我还对你的代码进行了一些修改和优化:

  • 没有必要根据条件再次遍历订单项目,而是将条件放入循环中
  • $item->get_quantity()< /code> 可用于了解数量
  • 根据产品 ID,可以将多个角色分配给同一用户,

因此您得到:

function action_woocommerce_order_status_completed( $order_id, $order ) {
    // Product IDs
    $item_a = 30;
    $item_b = 813;

    // Is a order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get user
        $user = $order->get_user();

        // User is NOT empty
        if ( ! empty ( $user ) ) {
            // Loop through order items
            foreach ( $order->get_items() as $key => $item ) {
                // Item A
                if ( $item->get_product_id() == $item_a ) {
                    // Add user role
                    $user->add_role( 'user-role-a' );
                // Item B
                } elseif ( $item->get_product_id() == $item_b ) {
                    // Add user role
                    $user->add_role( 'user-role-b' );

                    // Quantity equal to or greater than
                    if ( $item->get_quantity() >= 2 ) {
                        // Add user role
                        $user->add_role( 'user-role-c' );                       
                    }
                }
            }
        }
    }
}
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 2 );

In addition to answering your question, I also modified and optimized your code a bit:

  • It is not necessary to go through the order items again per condition, instead put the conditions in the loop
  • $item->get_quantity() can be used to know the quantity
  • Multiple roles can be assigned to the same user, based on the product IDs

So you get:

function action_woocommerce_order_status_completed( $order_id, $order ) {
    // Product IDs
    $item_a = 30;
    $item_b = 813;

    // Is a order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get user
        $user = $order->get_user();

        // User is NOT empty
        if ( ! empty ( $user ) ) {
            // Loop through order items
            foreach ( $order->get_items() as $key => $item ) {
                // Item A
                if ( $item->get_product_id() == $item_a ) {
                    // Add user role
                    $user->add_role( 'user-role-a' );
                // Item B
                } elseif ( $item->get_product_id() == $item_b ) {
                    // Add user role
                    $user->add_role( 'user-role-b' );

                    // Quantity equal to or greater than
                    if ( $item->get_quantity() >= 2 ) {
                        // Add user role
                        $user->add_role( 'user-role-c' );                       
                    }
                }
            }
        }
    }
}
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文