php - 订单状态完成时更改购买特定产品的用户角色

标签 php wordpress woocommerce orders hook-woocommerce

因此,我帮助某人启动了一个网站,当有人购买了特定产品时,他们想要打折的产品。我找到了一个解决方案并实现了它,它在网站启动时发挥了作用,并且在客户购买产品时不再改变他们的角色。我试图从 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_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 种特定产品中的一种,他的角色将从 'customer' 更改为 'author' 当订单状态设置为 'completed' 时,我建议您尝试使用此电子邮件通知 Hook (如果第一个代码片段不起作用)。

例如,我在这里使用 woocommerce_email_before_order_table Hook ,它将在某些条件的帮助下执行并触发“已完成订单客户电子邮件通知”。
(此代码还与下一个即将发布的主要 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 文件中。

关于php - 订单状态完成时更改购买特定产品的用户角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510007/

相关文章:

jquery - 找到子图像宽度并将其应用于包含父div jquery

javascript - 如何制作将从 MySQL 数据库中删除表行的 JavaScript 函数

php - CKEditor 在支持 prototype.js 的环境中是否正常工作?

css - 在 Wordpress 中的第 3 方插件 css 之后加载自定义 css

css - 图标在 Divi Child Theme 中无法正确显示

java - 使用 Apache HttpClient 的 Https 和基本身份验证

php - 更改 WooCommerce 'xyz' 类别页面上显示的产品数量

wordpress - 下单后设置下一个续订付款日期的最可靠 Hook

php - 如何将 css 样式放入 php "echo' ed"html table td?

php - 在 MySQL 中选择日期范围在新年后不起作用