2017-05-04 2 views
1

Ich möchte die niedrigsten einfache Produkt Preis in der content-oroduct_cat.php Seite anzeigen. Der folgende Code von Fancy Squares funktioniert, um den niedrigsten Preis zu zeigen, aber ich möchte nur Einfache Produkte zeigen, d. H. Keine gruppierten Produkte auslassen.Die niedrigsten simple Produkt Preise in der Kategorie anzeigen Seite

/* SHOW LOWEST PRICE ON CATEGORY PAGE */ 
//woocommerce get lowest price in category 
function wpq_get_min_price_per_product_cat($term_id) 
{  
    global $wpdb; 

    $sql = " 
    SELECT MIN(meta_value+0) as minprice 
    FROM {$wpdb->posts} 
    INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) 
    INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
    WHERE 
     ({$wpdb->term_relationships}.term_taxonomy_id IN (%d)) 
    AND {$wpdb->posts}.post_type = 'product' 
    AND {$wpdb->posts}.post_status = 'publish' 
    AND {$wpdb->postmeta}.meta_key = '_price' 
    "; 

    return $wpdb->get_var($wpdb->prepare($sql, $term_id)); 
} 

ich versucht, mit:

AND {$wpdb->posts}.product_type = 'simple' 

aber das hat nicht funktioniert. Wie würde ich nur einfache Produkte anzeigen?

Antwort

1

Sie Abfrage funktioniert nicht, weil product_type nicht in posts Tabelle gespeichert ist es in term_taxonomy Tabelle gespeichert ist. Um die gewünschte zu erhalten, müssen Sie Sub-Abfrage verwenden, die alle einfachen Produkt und die Hauptabfrage Filter es nach Kategorie abholen wird.

Ich habe Ihr wpq_get_min_price_per_product_cat() wie unten

function wh_get_min_price_per_product_cat($term_id) 
{ 
    global $wpdb; 

    $sql = " 
    SELECT MIN(meta_value+0) as minprice 
    FROM {$wpdb->posts} 
    INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) 
    INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
    WHERE 
     ({$wpdb->term_relationships}.term_taxonomy_id IN (%d)) 
     AND {$wpdb->posts}.post_type = 'product' 
     AND {$wpdb->posts}.post_status = 'publish' 
     AND {$wpdb->postmeta}.meta_key = '_price' 
     AND {$wpdb->posts}.ID IN (SELECT posts.ID 
       FROM {$wpdb->posts} AS posts 
       INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id 
       INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id 
       INNER JOIN {$wpdb->terms} AS terms ON term_taxonomy.term_id = terms.term_id 
       WHERE term_taxonomy.taxonomy = 'product_type' 
       AND terms.slug = 'simple' 
       AND posts.post_type = 'product')"; 
    return $wpdb->get_var($wpdb->prepare($sql, $term_id)); 
} 

-Code geht in functions.php Datei Ihres aktiven Kind Thema (oder Thema) geändert. Oder auch in beliebigen Plugin-PHP-Dateien.

USAGE

echo wh_get_min_price_per_product_cat($cat_id); 

-Code und Werke getestet.

Referenz: SQL query to check product_type in WooCommerce

hoffe, das hilft!

+0

Danke Raunak - hat ein Vergnügen! – Renegade

Verwandte Themen