Meine Website verwendet WordPress, "den Kalender Ereignisse" und "woocommerce" und ich verwalte Rollen und Funktionen mit "Advanced Access Manager" Plugin.Kunden je nach Produktkategorie
Ich versuche eine Funktion hinzuzufügen, die der Rolle eines Käufers basierend auf der Produktkategorie, die sie gerade gekauft haben, eine Funktion hinzufügt.
Also versuche ich, um das Produkt zu erhalten (gerade gekauft haben) oder die Ereigniskategorie aus dem Benutzer-ID, aber etwas schief läuft und es ist fast unmöglich, zu Test Schritt für Schritt, weil der Prozess beenden zu müssen, lassen die Funktion arbeiten. Hier
ist der Code:
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;
}
So zum Beispiel ein Kunde ein Produkt mit der Kategorie „course_paris_night“ kaufen und so sollte es die Rolle Fähigkeit „course_paris_night“ haben. Mit dieser Fähigkeit wird dieser Kunde auf Tutorial für die gleiche Kategorie mit einem Beispiel zugreifen.
Danke für jede Hilfe auf dem Code oder auf dem Weg, es zu testen! (müssen lernen!)
SO !!! 24 April ... wenige Tage später:
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;
}
}
Aber ich bekomme keine guten Ergebnisse. So testet wieder hier ist es, was ich bekommen hat Benutzer Info:
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 hat meinen Benutzer kauft zwei neuen Kurs unter der Kategorie „mycourse saint-malo intermédiaire“ und „mycourse paris avancé“ Ich soll unter haben „[ Rollen] => Array "die zwei neuen Rollen ... aber haben Sie sehen können, ist es nicht.
Mein Code fügt die beiden neuen Rollen in [caps] und [allcaps] hinzu, aber nicht in [roles] !!!!
Ich bin mir sicher, dass ich sehr nahe bin. Irgendeine Idee ?
// UPDATE //
Leider !!! Ich habe vergessen, einige wichtige Schritte, und ich entdecken schließlich, was nicht in meinem Test-Code arbeitete (ich es Testen Sie direkt auf einer Seite nicht in einer Funktion ... wird das morgen tun ... hier ist es 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;
}
}
}
Dies ist nicht einfach für mich, genau zu folgen, was Sie fragen, um ehrlich zu sein –
das ist, was ich verstehe. Nachdem ein Kunde seine Bestellung abgeschlossen hat, möchten Sie ein Login-Konto für sie erstellen und ihre Rolle basierend auf der Kategorie des gekauften Produkts haben. Und diese Rolle bezieht sich direkt auf eine Lernprogrammkategorie, die denselben Namen hat. – imvain2
Angenommen, Sie möchten Ihr Lernprogramm auf die Personen beschränken, die Ihren Kurs kaufen, aber nur auf das Lernprogramm zugreifen, das der Kategorie des von ihnen gekauften Produkts entspricht: ex: Ich kaufe einen Kurs "basic", auf den ich zugreifen kann. grundlegende "Tutorial" und nicht "fortgeschrittene". (Verwendung eines Plugins zur Verwaltung des Zugriffs basierend auf Benutzerrolle UND Fähigkeiten) Ein Kunde ist also als "Kundenrolle" registriert und hat keinen Zugriff auf "Basis-Tutorial", da er dafür "Basic" in seiner Fähigkeit benötigt. –