Change the user role on purchase for specific products when order status is completed

后端 未结 1 1646
-上瘾入骨i
-上瘾入骨i 2021-01-20 15:54

So I helped someone launch a site and they wanted a discounted product when someone purchased a specific product. I found a solution and implemented it and it worked at laun

1条回答
  •  囚心锁ツ
    2021-01-20 16:35

    UPDATE

    Normally this updated code version should work with woocommerce_order_status_completed and then you should try this code before.
    (This code is also compatible with next upcoming major WooCommerce update 2.7).

    Here is the code:

    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;
                }
            }
        }
    }
    

    But as I don't know how your order is changed to 'completed' status, if you want to be sure (in all possible cases) that the customer that will buy one of your 2 specific products, will have his role changed from 'customer' to 'author' when order status is set to 'completed', I will recommend you trying to use this an email notification hook (if the first code snippet doesn't work).

    For example here I use woocommerce_email_before_order_table hook, that will be executed and fired on "Completed order customer email notification" with the help of some conditions.
    (This code is also compatible with next upcoming major WooCommerce update 2.7).

    Here is your revisited and tested code:

    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;
                    }
                }
            }
        }
    }
    

    Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

    0 讨论(0)
提交回复
热议问题