第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

WooCommerce:完成一定數(shù)量的訂單或一個(gè)訂單中的最低金額后自動(dòng)更改用戶角色

WooCommerce:完成一定數(shù)量的訂單或一個(gè)訂單中的最低金額后自動(dòng)更改用戶角色

PHP
翻過高山走不出你 2023-11-03 15:30:31
我想在之后自動(dòng)將用戶角色更改為高級(jí)用戶角色3已完成訂單一筆訂單消費(fèi)滿500現(xiàn)金我在互聯(lián)網(wǎng)上找到了這兩個(gè)代碼片段:function change_privategroup_on_purchase( $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 ) {              // Insert the Product IDs Here. If Multiple Products seperate by ||      if ( $product_id == '7026' || $product_id == '7027' || $product_id == '7028' || $product_id == '7029') {                  // Mention the group number below. Here it is group1.                  update_user_meta( $order->user_id, 'private_group', 'group1');      }    }}}add_action( 'woocommerce_order_status_processing', 'change_privategroup_on_purchase' );這是訪問訂單歷史記錄的一個(gè):function has_bought() {    // Get all customer orders    $customer_orders = get_posts( array(        'numberposts' => 3, // one order is enough        'meta_key'    => '_customer_user',        'meta_value'  => get_current_user_id(),        'post_type'   => 'shop_order', // WC orders post type        'post_status' => 'wc-completed', // Only orders with "completed" status        'fields'      => 'ids', // Return Ids "completed"    ) );    // return "true" when customer has already at least one order (false if not)   return count($customer_orders) > 3 ?  true : false; }我如何調(diào)整它們以使其發(fā)揮作用?
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

  • 獲取by數(shù)量的自定義函數(shù)completed ordersuser id基于get_order_count( &$customer );


/**

?* Return the number of orders this customer has.

?*

?* @since 3.0.0

?* @param WC_Customer $customer Customer object.

?* @return integer

?*/

public function get_order_count( &$customer ) {

? ? $count = get_user_meta( $customer->get_id(), '_order_count', true );


? ? if ( '' === $count ) {

? ? ? ? global $wpdb;


? ? ? ? $count = $wpdb->get_var(

? ? ? ? ? ? // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared

? ? ? ? ? ? "SELECT COUNT(*)

? ? ? ? ? ? FROM $wpdb->posts as posts

? ? ? ? ? ? LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id

? ? ? ? ? ? WHERE? ?meta.meta_key = '_customer_user'

? ? ? ? ? ? AND? ? ?posts.post_type = 'shop_order'

? ? ? ? ? ? AND? ? ?posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )

? ? ? ? ? ? AND? ? ?meta_value = '" . esc_sql( $customer->get_id() ) . "'"

? ? ? ? ? ? // phpcs:enable

? ? ? ? );

? ? ? ? update_user_meta( $customer->get_id(), '_order_count', $count );

? ? }


? ? return absint( $count );

}

所以我們得到

  • 條件可以通過設(shè)置進(jìn)行調(diào)整

  • usermeta 表中使用了一個(gè)標(biāo)志,因此如果一個(gè)人已經(jīng)是高級(jí)會(huì)員,則整個(gè)代碼不會(huì)不必要地重新運(yùn)行

  • woocommerce_order_status_completed使用鉤子 - 此鉤子在 Woocommerce 訂單完成后更改字段值

  • 通過代碼中添加的注釋進(jìn)行解釋

// Based on https://github.com/woocommerce/woocommerce/blob/85a077b939b44500fc01b68d34e711117545f586/includes/data-stores/class-wc-customer-data-store.php#L349-L377

function total_completed_orders( $the_user_id ) {

? ? global $wpdb;

? ??

? ? $count = $wpdb->get_var(

? ? ? ? // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared

? ? ? ? "SELECT COUNT(*)

? ? ? ? FROM $wpdb->posts as posts

? ? ? ? LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id

? ? ? ? WHERE? ?meta.meta_key = '_customer_user'

? ? ? ? AND? ? ?posts.post_type = 'shop_order'

? ? ? ? AND? ? ?posts.post_status = 'wc-completed'

? ? ? ? AND? ? ?meta_value = '" . esc_sql( $the_user_id ) . "'"

? ? ? ? // phpcs:enable

? ? );


? ? // Return a boolean value based on orders count

? ? return $count;

}


function action_woocommerce_order_status_completed( $order_id ) {

? ? /*** Settings ***/

? ??

? ? // User role in which the customer role must be changed

? ? $premium_user_role = 'premium';

? ??

? ? // Spending min cash in one order

? ? $min_in_one_order = 500;

? ??

? ? // Change role after x completed orders

? ? $change_role_after_x_completed_orders = 3;

? ??

? ? /*** End settings ***/

? ??

? ? $flag = false;

? ??

? ? // Get an instance of the WC_Order object

? ? $order = wc_get_order( $order_id );

? ??

? ? // Is a order

? ? if( is_a( $order, 'WC_Order' ) ) {

? ? ? ? // Get user ID or $order->get_customer_id();

? ? ? ? $user_id = $order->get_user_id();

? ? ? ??

? ? ? ? // Retrieve user meta field for a user.?

? ? ? ? $is_premium = get_user_meta( $user_id, '_customer_is_premium_member', true );

? ? ? ??

? ? ? ? // Is NOT a premium member

? ? ? ? if ( $is_premium != 1 ) {

? ? ? ? ? ? // Count total completed orders

? ? ? ? ? ? $total_completed_orders = total_completed_orders( $user_id );

? ? ? ? ? ??

? ? ? ? ? ? // Total completed orders greater than or equal to change role after x completed orders

? ? ? ? ? ? if ( $total_completed_orders >= $change_role_after_x_completed_orders ) {

? ? ? ? ? ? ? ? $flag = true;

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? // Get current order total

? ? ? ? ? ? ? ? $order_total = $order->get_total();

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? // Order total greater than or equal to minimum in one order

? ? ? ? ? ? ? ? if ( $order_total >= $min_in_one_order ) {

? ? ? ? ? ? ? ? ? ? $flag = true;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? // True, at least 1 condition has been met

? ? ? ? ? ? if ( $flag ) {

? ? ? ? ? ? ? ? // Get the user object

? ? ? ? ? ? ? ? $user = get_userdata( $user_id );


? ? ? ? ? ? ? ? // Get all the user roles as an array.

? ? ? ? ? ? ? ? $user_roles = $user->roles;

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? // User role contains 'customer'

? ? ? ? ? ? ? ? if ( in_array( 'customer', $user_roles ) ) {

? ? ? ? ? ? ? ? ? ? // Remove customer role

? ? ? ? ? ? ? ? ? ? $user->remove_role( 'customer' );

? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? // Add premium role

? ? ? ? ? ? ? ? ? ? $user->add_role( $premium_user_role );

? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? // Update user meta field based on user ID, set true

? ? ? ? ? ? ? ? ? ? update_user_meta( $user_id, '_customer_is_premium_member', 1 );

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );



查看完整回答
反對 回復(fù) 2023-11-03
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)