我的网站使用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
假设你想限制你的教程给购买你的课程的人,但你只希望他们只访问与他们购买的产品类别对应的教程:例如:我购买一门“基础课程”,这样我就可以访问“基本“教程而不是”高级“教程。 (使用插件根据用户角色和功能管理访问)因此,客户被注册为“客户角色”,并且无法访问“基本教程”,因为他需要“基本”功能。 –