【问题标题】:Change the user role on purchase for specific products when order status is completed订单状态完成后更改特定产品购买的用户角色
【发布时间】:2017-01-06 16:31:59
【问题描述】:

所以我帮助某人启动了一个网站,当有人购买特定产品时,他们想要打折的产品。我找到了一个解决方案并实施了它,它在网站启动时起作用,并且在客户购买产品时不再改变他们的角色。我试图从 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' );

【问题讨论】:

    标签: php wordpress woocommerce orders hook-woocommerce


    【解决方案1】:

    更新

    通常,此更新的代码版本应该与 woocommerce_order_status_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' 状态,如果您想确定(在所有可能的情况下)客户将购买您的 2特定产品,当订单状态设置为 'completed' 时,他的角色将从 'customer' 更改为 'author',我会建议您尝试使用此电子邮件通知挂钩(如果第一个代码 sn-p 不起作用)。

    例如,这里我使用 woocommerce_email_before_order_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 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-29
      • 2017-03-18
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多