Nel processo di costruzione attorno a Easy Digital Downloads potresti trovare la necessità di interrogare il database per dati sui prodotti. Questo documento mostrerà diversi esempi di come farlo correttamente.
Ottenere un Singolo Prodotto
Il modo migliore per ottenere informazioni su un singolo prodotto è usare la classe EDD_Download. Accetta un
download_id e restituisce un oggetto con informazioni sul download. Esempio:
$download = new EDD_Download( 109 );
Questo restituirebbe un oggetto come questo:
EDD_Download Object
(
[ID] => 109
[price:EDD_Download:private] =>
[prices:EDD_Download:private] =>
[files:EDD_Download:private] =>
[file_download_limit:EDD_Download:private] =>
[type:EDD_Download:private] =>
[bundled_downloads:EDD_Download:private] =>
[sales:EDD_Download:private] =>
[earnings:EDD_Download:private] =>
[notes:EDD_Download:private] =>
[sku:EDD_Download:private] =>
[button_behavior:EDD_Download:private] =>
[post_author] => 2
[post_date] => 2016-03-22 13:30:29
[post_date_gmt] => 2016-03-22 13:30:29
[post_content] => Buy my mp3!
[post_title] => My MP3
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => my-mp3
[to_ping] =>
[pinged] =>
[post_modified] => 2016-03-22 13:30:29
[post_modified_gmt] => 2016-03-22 13:30:29
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://edd.dev/?post_type=download&p=109
[menu_order] => 0
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[post_type] => download
)
Una volta ottenuto l'oggetto, ci sono un'ampia varietà di metodi che possono essere eseguiti su di esso per ottenere informazioni come prezzo, variazioni, ecc.
Documentazione completa per EDD_Download qui.
Ottenere Prodotti Multipli
I download sono un tipo di contenuto personalizzato di WordPress, quindi è possibile ottenere gli ID di più download usando
get_posts(). Se specifichi correttamente il parametro fields, get_posts otterrà solo gli ID e la query sarà molto più veloce. Dovrai anche specificare almeno download per il post_type. Esempio:
$args = array(
'fields' => 'ids',
'post_type' => 'download',
);
$downloads = get_posts( $args );
Puoi usare qualsiasi opzione desideri in
get_posts() per restringere la tua query.
Documentazione completa per get_posts() qui.
Un'opzione aggiuntiva non trovata nella documentazione per
get_posts() si chiama no_found_rows(). Questo rimuove alcuni dati necessari per la paginazione, ma accelera notevolmente la tua query. Se hai bisogno della paginazione, non usare questa opzione. Se non hai bisogno della paginazione, apparirebbe così:
$args = array(
'fields' => 'ids',
'post_type' => 'download',
'no_found_rows' => true,
);
$downloads = get_posts( $args );
La query sopra restituirà un array di download_id come questo:
Array
(
[0] => 109
[1] => 82
[2] => 79
[3] => 61
[4] => 42
)
Lavorare con Prodotti Multipli
Una volta che hai un array di download_id, puoi semplicemente iterare sull'array creando oggetti EDD_Download come questo:
foreach ( $downloads as $key => $download_id ) {
$download = new EDD_Download( $download_id );
// do whatever you wish with each object
}
