2017-03-02 4 views
3

Ich möchte eine API-Anfrage senden, nachdem das Produkt von Woocommerce Admin hinzugefügt. Eigentlich möchte ich, dass wenn ein Benutzer ein neues Produkt (A-Kurs) zu seinem Shop hinzufügt und die API-Anfrage einen Kurs mit dem gleichen Namen wie das Produkt im LMS erstellt.Holen Sie sich kürzlich Produkt mit Hilfe von Woocommerce Hook

Es ist mir gelungen, ein Produkterstellungs-Ereignis anzuhängen, aber ich weiß nicht, wie ich Daten des Produkts, das ich in Woocommerce erstellt oder hinzugefügt habe, erhalten kann.

Hier ist mein Code:

add_action('transition_post_status', 'product_add', 10, 3); 
function product_add($new_status, $old_status, $post) { 
if( 
     $old_status != 'publish' 
     && $new_status == 'publish' 
     && !empty($post->ID) 
     && in_array($post->post_type, 
      array('product') 
      ) 
     ) { 
      //here I want to get the data of product that is added 
      } 
} 

dieser Code funktioniert gut, wenn ich ein Produkt und Echo etwas in dieser Funktion hinzufügen, es funktioniert gut.

Ich möchte nur den Namen und die ID des Produkts erhalten.

Danke.

Antwort

1

An diesem Punkt ist es sehr einfach, alle verwandten Daten zu Ihrem veröffentlichten Produkt zu erhalten und noch mehr, um nur die Produkt-ID und den Produktnamen zu erhalten. Im Folgenden finden Sie die meisten aller Möglichkeiten finden, die Sie haben alle damit verbundene Daten von Ihrem Produkt zu erhalten:

add_action('transition_post_status', 'action_product_add', 10, 3); 
function action_product_add($new_status, $old_status, $post){ 
    if('publish' != $old_status && 'publish' != $new_status 
     && !empty($post->ID) && in_array($post->post_type, array('product'))){ 

     // You can access to the post meta data directly 
     $sku = get_post_meta($post->ID, '_sku', true); 

     // Or Get an instance of the product object (see below) 
     $product = wc_get_product($post->ID); 

     // Then you can use all WC_Product class and sub classes methods 
     $price = $product->get_price(); // Get the product price 

     // 1°) Get the product ID (You have it already) 
     $product_id = $post->ID; 
     // Or (compatibility with WC +3) 
     $product_id = method_exists($product, 'get_id') ? $product->get_id() : $product->id; 

     // 2°) To get the name (the title) 
     $name = $post->post_title; 
     // Or 
     $name = $product->get_title(); 
    } 
} 

-Code geht in function.php Datei Ihres aktiven Kind Thema (oder Thema) oder auch in jeder Plugin-Datei .

Alles ist getestet und funktioniert.


Referenz: Class WC_Product methods

Verwandte Themen