2016-11-08 1 views
2

Ich möchte überprüfen, ob ein Benutzer die Erlaubnis hat oder nicht, irgendeine Datei herunterzuladen. Ich habe Produkt-ID und Benutzer-ID so, wie kann ich prüfen?Überprüfen Sie, ob der Benutzer die Erlaubnis hat oder nicht, eine Datei in WooCommerce herunterzuladen

Ich habe viel auf Google und in Woocommerce-Dokumentation erkundet, aber keine Lösung gefunden.

Irgendwelche Hilfe?

Vielen Dank im Voraus.

+0

Ich habe https://github.com/woocommerce/woocommerce/issues/12275#issuecomment-259122914 diesen Link von WooCommerce Unterstützung, kann jemand helfen? –

Antwort

2

Hier ist der Prozess, um diese Informationen zu erhalten, die Sie in jeder Funktion oder Hooked-Funktion in Ihren PHP-Dateien verwenden können. Hier

ist der Code:

// Get all current customer orders 
$customer_orders = wc_get_orders($args = array(
    'numberposts' => -1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(),// for current user id 
    'post_status' => array_keys(wc_get_order_statuses()), 
)); 

// The different loops to get the downloadable products bought by this user 
foreach ($customer_orders as $customer_order){ 
    if (!empty($customer_orders)){ 
     foreach ($customer_orders as $customer_order){ 
      if(!$customer_order->has_downloadable_item() && $customer_order->is_paid()){ 
       foreach($customer_order->get_items() as $item){ 
        $item_id = $item['product_id']; // product ID 

        // Just the downloadable items Below 
        if($item->is_download_permitted()) { 
         // do something because the download is permitted 

         // you can use different methods on the $customer_product object: 
         // Get the downloadbles files (array): 
         $downloadable_files_array = get_item_downloads($item); 
         // Display download links for an order item. 
         $display_item_downloads = display_item_downloads($item); 
         // Get the Download URL of a given $download_id 
         // $download_url = get_download_url($item_id, $download_id) 

        } else { 
         // do something because the download is NOT permitted 
        } 
       } 
      } 
     } 
    } 
} 

-Code geht in beliebiger PHP-Datei Ihres aktiven Kind Thema (oder Thema) oder auch in beliebiger Plugin PHP-Dateien.

Dies ist getestet und voll funktionsfähig.

Referenz: WC_Abstract_Order class - Methods

Verwandte Themen