编程语言
首页 > 编程语言> > php-完成订单状态后,更改特定产品的购买用户角色

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

作者:互联网

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

但是由于我不知道您的订单如何更改为“已完成”状态,因此,如果您想确保(在所有可能的情况下)将购买您的2种特定产品之一的客户的角色将从“当订单状态设置为“已完成”时,“客户”为“作者”,我建议您尝试使用此电子邮件通知挂钩(如果第一个代码段不起作用).

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

标签:orders,woocommerce,hook-woocommerce,wordpress,php
来源: https://codeday.me/bug/20191026/1936018.html