Php 在订单状态完成时更改特定产品的用户角色

Php 在订单状态完成时更改特定产品的用户角色,php,wordpress,woocommerce,orders,hook-woocommerce,Php,Wordpress,Woocommerce,Orders,Hook Woocommerce,所以我帮助一些人建立了一个网站,当有人购买某个特定的产品时,他们想要一个打折的产品。我找到了一个解决方案并实施了它,它在网站发布时起到了作用,不再改变客户在购买产品时的角色。我试图从Woothemes获得支持,但他们不支持定制,希望他们购买129美元的扩展来处理这个问题 有没有人有一个仍然有效的解决方案 这是我的密码: // Update User on purchase https://gist.github.com/troydean/9322593 function lgbk_add_me

所以我帮助一些人建立了一个网站,当有人购买某个特定的产品时,他们想要一个打折的产品。我找到了一个解决方案并实施了它,它在网站发布时起到了作用,不再改变客户在购买产品时的角色。我试图从Woothemes获得支持,但他们不支持定制,希望他们购买129美元的扩展来处理这个问题

有没有人有一个仍然有效的解决方案

这是我的密码:

// Update User on purchase https://gist.github.com/troydean/9322593 
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 == '247' || $order->user_id > 0 && $product_id == '255') {
        update_user_meta( $order->user_id, 'paying_customer', 1 );
        $user = new WP_User( $order->user_id );

        // Remove role
        $user->remove_role( 'customer' ); 

        // Add role
        $user->add_role( 'author' );
    }
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );

更新

通常,此更新的代码版本应适用于
woocommerce\u order\u status\u completed
,然后您应在之前尝试此代码
(此代码也与即将发布的WooCommerce主要更新2.7兼容)

代码如下:

add_action( 'woocommerce_order_status_completed', 'custom_action_on_completed_customer_email_notification' );
function custom_action_on_completed_customer_email_notification( $order_id ) {

    // Set HERE your targetted products IDs:
    $targetted_products = array( 247, 255 );

    $order = wc_get_order( $order_id );

    if ( $order->get_user_id() > 0 ) {
        foreach ( $order->get_items() as $order_item ) {
            // Here we detect if the a target product is part of this order items
            if ( in_array( $order_item['product_id'], $targetted_products ) ){

                // I think tha this is not really needed as it's set when an order has been paid…
                update_user_meta( $order->get_user_id(), 'paying_customer', 1 ); // 1 => true

                // Remove all roles and set 'editor' as user role (for current user)
                $user = new WP_User( $order->get_user_id() );
                $user->set_role( 'author' );

                // Product is found, we break the loop…
                break;
            }
        }
    }
}
但由于我不知道您的订单如何更改为“已完成”状态,如果您想确定(在所有可能的情况下)将购买您的两种特定产品之一的客户,将其角色从
更改为
当订单状态设置为
'completed'
时,我建议您尝试使用此电子邮件通知挂钩(如果第一个代码段不起作用)

例如,我在这里使用了
woocommerce\u email\u before\u order\u table
钩子,在某些条件的帮助下,该钩子将在“已完成订单客户电子邮件通知”时执行并触发
(此代码也与即将发布的WooCommerce主要更新2.7兼容)

以下是您重新访问和测试的代码:

add_action( 'woocommerce_email_before_order_table', 'custom_action_on_completed_customer_email_notification', 10, 4 );
function custom_action_on_completed_customer_email_notification( $order, $sent_to_admin, $plain_text, $email ) {

    if( 'customer_completed_order' == $email->id ){

        // Set HERE your targetted products IDs:
        $targetted_products = array( 247, 255 );

        if ( $order->get_user_id() > 0 ) {
            foreach ( $order->get_items() as $order_item ) {
                // Here we detect if the a target product is part of this order items
                if ( in_array( $order_item['product_id'], $targetted_products ) ){

                    // I think tha this is not really needed as it's set when an order has been paid…
                    update_user_meta( $order->get_user_id(), 'paying_customer', 1 ); // 1 => true

                    // Remove all roles and set 'editor' as user role (for current user)
                    $user = new WP_User( $order->get_user_id() );
                    $user->set_role( 'author' );

                    // Product is found, we break the loop…
                    break;
                }
            }
        }
    }
}
代码进入活动子主题(或主题)的function.php文件。或者在任何插件php文件中