我的網站使用wordpress,「活動日曆」插件和「woocommerce」,並使用「高級訪問管理器」插件管理角色和功能。根據產品分類爲客戶添加功能
我嘗試添加一個函數,該函數將根據他們剛剛購買的產品類別爲買方的角色添加功能。
所以我試圖讓產品(剛買)或用戶ID的事件類別,但事情錯了,它幾乎是不可能的測試一步一步,因爲這個過程必須完成,讓功能工作。
下面是代碼:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
$order = new WC_Order($order_id);
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
// need to test that :
$beta= str_replace(
':',
'',
tribe_get_event_categories($event->ID, array(
'echo' => false,
'label' => '',
' label_before' => '',
'label_after' => '',
'wrap_before' => '',
'wrap_after' => '',
)));
$beta = strip_tags($beta);
foreach ($items as $item) {
//check if user already have capabilitie
if (! user_can($user_id, $capability)){
//add capability to specific user
$user = new WP_User($user_id);
$user->add_cap($beta);
}
}
return $order_id;
}
因此,例如,一個客戶購買產品與類別「course_paris_night」,所以它應有的作用能力「course_paris_night」。憑藉此功能,該客戶將通過示例訪問相同類別的教程。
感謝您對代碼或測試方式的任何幫助! (需要學習!)
因此!!! 4月24日...幾天後:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
global $current_user;
global $wp_roles;
$order = new WC_Order($item->ID);
$order->populate($item);
$items = $order->get_items();
foreach ($items as $key => $item2) :
$event_id_in_product = absint($item2['product_id']);
endforeach;
// trouve la catégorie du produit acheté par le stagiaire
// Get all terms associated with post in woocommerce's taxonomy 'product_cat'
$terms = get_the_terms($event_id_in_product, 'product_cat');
//Get an array of their IDs
$term_ids = wp_list_pluck($terms,'term_id');
//Get array of parents - 0 is not a parent
$parents = array_filter(wp_list_pluck($terms,'parent'));
//Get array of IDs of terms which are not parents.
$term_ids_not_parents = array_diff($term_ids, $parents);
//Get corresponding term objects.
$terms_not_parents = array_intersect_key($terms, $term_ids_not_parents);
//Extract the name of the category from the array and post it.
foreach($terms_not_parents as $term_not_parent){
$beta = $term_not_parent->name;
$alpha = strip_tags($beta);
$alpha = trim(strip_tags($alpha));
$alpha = str_replace(' - ',' ',$alpha);
$alpha = strtolower($alpha);
$all_roles = $wp_roles->roles;
$user = new WP_User($current_user->ID);
$result = $user->add_role($alpha);
return $order_id;
}
}
但我沒有得到任何好的結果。因此,測試再這裏是我所得到的具有用戶信息:
WP_User Object
(
[data] => stdClass Object
(
[ID] => 50
[user_login] => buyer.new
[user_pass] => $P$BKlx7.CzfvnLL43EZaZdmo84shZQo41
[user_nicename] => buyer-new
[user_email] => [email protected]
[user_url] =>
[user_registered] => 2016-04-23 22:05:27
[user_activation_key] =>
[user_status] => 0
[display_name] => New
)
[ID] => 50
[caps] => Array
(
[mycourse saint-malo intermédiaire] => 1
[mycourse paris avancé] => 1
)
[cap_key] => lep_capabilities
[roles] => Array
(
[0] => mycourse saint-malo intermédiaire
)
[allcaps] => Array
(
[read] => 1
[level_0] => 1
[read_private_tribe_organizers] => 1
[mycourse saint-malo intermédiaire] => 1
[mycourse paris avancé] => 1
)
[filter] =>
)
Normaly有我的用戶是買了兩個新課程下的類別「mycourse聖馬洛intermédiaire」和「mycourse巴黎AVANCE」我應該在「[角色] =>陣列「兩個新的角色......但你可以看到它不是。
我的代碼將兩個新角色添加到[caps]和[allcaps]中,但不添加到[roles]中!
我相信我非常接近。任何想法 ?
// UPDATE //
對不起!我忘了一些重要的步驟,我終於發現什麼沒有在我的測試代碼的工作(我直接測試它的網頁無法在功能上...會做,明天...這裏是上午02點03分)
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
global $current_user;
global $wp_roles;
$all_orders = get_posts(array(
'numberposts' => -1,
//'meta_key' => '_customer_user',
//'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'completed',
));
$args = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'completed',
));
if(! empty($args)){
foreach($args as $item) {
setup_postdata($item);
$order = new WC_Order($item->ID);
$order->populate($item);
$items = $order->get_items();
foreach ($items as $key => $item2) :
$event_id_in_product = absint($item2['product_id']);
endforeach;
//trouve la catégorie du produit acheté par le stagiaire
//Get all terms associated with post in woocommerce's taxonomy 'product_cat'
$terms = get_the_terms($event_id_in_product, 'product_cat');
//Get an array of their IDs
$term_ids = wp_list_pluck($terms,'term_id');
//Get array of parents - 0 is not a parent
$parents = array_filter(wp_list_pluck($terms,'parent'));
//Get array of IDs of terms which are not parents.
$term_ids_not_parents = array_diff($term_ids, $parents);
//Get corresponding term objects.
$terms_not_parents = array_intersect_key($terms, $term_ids_not_parents);
//Extract the name of the category from the array and post it.
foreach($terms_not_parents as $term_not_parent){
$beta = $term_not_parent->name;
$alpha = strip_tags($beta);
$alpha = trim(strip_tags($alpha));
$alpha = str_replace(' - ',' ',$alpha);
$alpha = str_replace('Stage','stagiaire',$alpha);
$alpha = strtolower($alpha);
$all_roles = $wp_roles->roles;
$user = new WP_User($current_user->ID);
$result = $user->add_role($alpha, $alpha, array($alpha =>true,)); // Array was missing in previous version !
return $order_id;
}
}
}
這對我來說並不容易,完全按照你的要求去做,老實說你 –
這就是我所理解的。客戶完成訂單後,您希望爲他們創建一個登錄帳戶,並根據所購產品的類別確定其角色。該角色直接與具有相同名稱的教程類別相關。 – imvain2
假設你想限制你的教程給購買你的課程的人,但你只希望他們只訪問與他們購買的產品類別對應的教程:例如:我購買一門「基礎課程」,這樣我就可以訪問「基本「教程而不是」高級「教程。 (使用插件根據用戶角色和功能管理訪問)因此,客戶被註冊爲「客戶角色」,並且無法訪問「基本教程」,因爲他需要「基本」功能。 –