2016-04-21 20 views
1
hinzufügen

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; 
} 
} 
} 
+0

Dies ist nicht einfach für mich, genau zu folgen, was Sie fragen, um ehrlich zu sein –

+0

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

+0

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. –

Antwort

0

Dies ist die Lösung und es funktioniert großartig unter meiner Testseite aber ...

// Retrieve customer order 
$blc_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($blc_args)){ 
foreach($blc_args as $blc_item) { 
    setup_postdata($item); 

    $blc_order = new WC_Order($blc_item->ID); 

    $blc_order->populate($blc_item); 

    $blc_items = $blc_order->get_items(); 

    foreach ($blc_items as $blc_key => $blc_item2) : 
    //print_r($item2); 
    $event_id_in_product = absint($blc_item2['product_id']); 

    //Get all terms associated with post in woocommerce's taxonomy 'product_cat' 
    $blc_terms = get_the_terms($event_id_in_product, 'product_cat'); 

    //echo"</br>"; 
    //echo "Event ID embed in product bought : " . $event_id_in_product; 
    //echo"</br>"; 

    //Get an array of their IDs 
    $blc_term_ids = wp_list_pluck($blc_terms,'term_id'); 

    //Get array of parents - 0 is not a parent 
    $blc_parents = array_filter(wp_list_pluck($blc_terms,'parent')); 

    //Get array of IDs of terms which are not parents. 
    $blc_term_ids_not_parents = array_diff($blc_term_ids, $blc_parents); 

    //Get corresponding term objects. 
    $blc_terms_not_parents = array_intersect_key($blc_terms, $blc_term_ids_not_parents); 

    //Extract the name of the category from the array and post it. 
    foreach($blc_terms_not_parents as $blc_term_not_parent){ 
     $blc_start = $blc_term_not_parent->name; 
    } 

    if(preg_match("/(Ultérieure|cadeau)/i", $blc_start)){ 

    }else{ 
     $beta = utf8_decode($blc_start); 
     $beta = trim(strip_tags($beta)); 
     $beta_without_accent = strtr($beta, utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'); 
     $beta_with_minuscule = strtolower($beta_without_accent); 
     $beta_with_minuscule = str_replace('balade - ','stagiaire_',$beta_with_minuscule); 
     $beta_with_underscore = str_replace(' - ', '_', $beta_with_minuscule); 
     $beta_stagiaire = str_replace('stage','stagiaire',$beta_with_underscore); 

     global $current_user; 

     $user = new WP_User($current_user->ID); 
     $user->add_role($beta_stagiaire); 
    } 
    endforeach; 
} 
} 

... ja ... aber! beim Bearbeiten und Parsen in meinen Funktionen unter meinem Kind-Thema tut es das nicht. Manchmal scheint es es zu verarbeiten, weil das letzte Echo (manchmal) erscheint und mir die richtige "Rolle zum Erstellen" zurücksendet.

Hier ist es der gleiche Code, sondern bauen in function.php umfassen:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 

function custom_process_order($order_id) { 
    global $current_user; 

    $order = wc_get_order($order_id); 
    $items = $order->get_items(); 

//  echo"</br>"; 
//  echo "Product ID : " . ($order->id); 

    //$order->populate($item); 
    //print_r($order); 

    $items = $order->get_items(); 

    foreach ($items as $key => $item2) : 
    //print_r($item2); 
    $event_id_in_product = absint($item2['product_id']); 

    //Get all terms associated with post in woocommerce's taxonomy 'product_cat' 
    $terms = get_the_terms($event_id_in_product, 'product_cat'); 

    //echo"</br>"; 
    //echo "Event ID embed in product bought : " . $event_id_in_product; 
    //echo"</br>"; 

    //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){ 
     $start = $term_not_parent->name;    
    } 

    //echo"</br>"; 
    //echo "Product Category : " . $start; 
    //echo"</br>"; 

    if(preg_match("/(Ultérieure|cadeau)/i", $start)){ 
//echo"</br>"; 
    //echo "Don't add that Product Category : " . $start; 
    //echo"</br>"; 
    }else{ 
    $beta = utf8_decode($start); 
    $beta = trim(strip_tags($beta)); 
    $beta_without_accent = strtr($beta, utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'); 
    $beta_with_minuscule = strtolower($beta_without_accent); 
    $beta_with_minuscule = str_replace('balade - ','stagiaire_',$beta_with_minuscule); 
    $beta_with_underscore = str_replace(' - ', '_', $beta_with_minuscule); 
    $beta_stagiaire = str_replace('stage','stagiaire',$beta_with_underscore); 

    echo"</br>"; 
    echo "Role to create : " . $beta_stagiaire; 
    echo"</br>"; 

    $user = new WP_User($current_user->ID); 

    $user->add_role($beta_stagiaire); 
    } 
    endforeach; 


return $order_id; 
} 

Und hat man sich vorstellen kann, es zu testen, es ist nicht so einfach, weil ich den Zahlungsprozess zu tun haben.

Irgendwelche Fehler, die ich irgendwo? Ich sehe es nicht.

+0

ARBEITSLÖSUNG: ... es funktioniert nicht in einer Funktion auf meinem Kind Thema, so dass ich es in einem benutzerdefinierten "danke.php" in meinem Kind Thema und es funktioniert 100% jetzt. Wenn Sie es für Ihre App verwenden möchten, passen Sie es einfach so an, wie ich den Rollen- und Kategorienamen bearbeite. Der Code ist kein sauberer Code und wenn jemand einen Weg findet ihn zu säubern und es als Funktion funktionieren zu lassen ... lass es mich wissen. –

+0

Vergessen Sie nicht, Ihren PHP als UTF8 NO BOM zu speichern ;-) –