2017-09-16 5 views
2

Je crée une extension de plugin pour un reporting plugin. Le plugin actuel crée divers rapports, y compris un rapport sur les produits les plus vendus sur une base par produit.WooCommerce: Obtenir le total des ventes pour les variations de produits dans une requête SQL

J'ai besoin de recréer cette configuration pour afficher les meilleurs produits en fonction de la variation. Les champs requis sont Nom du produit, SKU, Quantité vendue, Montant total des ventes.

Comment obtenir les données pour les variations de produits?

J'ai saisi mon code actuel ci-dessous.

requête SQL et le traitement des données:

$phoen_product_query = " 
     SELECT 
      posts.post_title AS product_name, 
      meta.meta_key as mkey, 
      meta.meta_value as product_value, 
      posts.ID AS ID 
      FROM {$wpdb->posts} AS posts 
      LEFT JOIN {$wpdb->postmeta} AS meta 
       ON posts.ID = meta.post_id 
     WHERE 
      posts.post_status IN ('publish','private') 
      AND posts.post_type IN ('product') 
      AND meta.meta_key IN ('total_sales' ,'_price' ,'post_views_count', '_sku') 
     ORDER BY 
      posts.ID ASC, 
      meta.meta_key ASC 
    "; 
    $phoen_product_data = $wpdb->get_results( $phoen_product_query,ARRAY_A); 

    foreach($phoen_product_data as $key1 => $valuee){ 

     if(!isset($phoen_top_products[$valuee['ID']])){ 

      $phoen_top_products[$valuee['ID']] = Array(); 

      $phoen_top_products[$valuee['ID']] = Array(

       "produc_total" => 0, 

       "product_price" => 0, 

       "product_count" => 0, 

       "product_views" => 0 

      ); 

     } 

     switch ($valuee['mkey']) { 

      case "_sku": 

       $phoen_top_products[$valuee['ID']]["product_sku"] = $valuee['product_value']; 

       break; 

      case "_price": 

       $phoen_top_products[$valuee['ID']]["product_price"] = $valuee['product_value']; 

       break; 

      case "post_views_count": 

       $phoen_top_products[$valuee['ID']]["product_views"] = $valuee['product_value']; 

       break; 

      case "total_sales": 

       $phoen_top_products[$valuee['ID']]["product_count"] = $valuee['product_value']; 

       $phoen_top_products[$valuee['ID']]["produc_total"] = $valuee['product_value'] * $phoen_top_products[$valuee['ID']]["product_price"]; 

       $phoen_top_products[$valuee['ID']]["product_name"] = $valuee['product_name']; 

       $phoen_top_products[$valuee['ID']]["ID"] = $valuee['ID']; 

       break; 

      default: 

       break; 

     } 

    } 

module Admin:

<table class="table table-striped table-bordered" id="phoen_top_product_table"> 
<thead> 
    <tr> 
    <th><?php _e('Product Name', 'advanced-reporting-for-woocommerce'); ?></th> 
    <th><?php _e('SKU', 'advanced-reporting-for-woocommerce'); ?></th> 
    <th><?php _e('Qty', 'advanced-reporting-for-woocommerce'); ?></th> 
    <th><?php _e('Amount', 'advanced-reporting-for-woocommerce'); ?></th> 
    </tr> 
</thead> 
<tbody> 
<?php 
$phoen_repot_product=array(); 
$phoen_product_count=0; 

for($i=0; $i<count($phoen_top_products); $i++) 
{ 
    if($phoen_get_product_val!='View All') 
    { 
     if($phoen_product_count<$phoen_get_product_val) 
     { 

      ?> 
      <tr class="phoen_top_product_tr"> 
       <td> 

        <?php $phoen_product_title=isset($phoen_top_products[$i]['product_name'])?$phoen_top_products[$i]['product_name']:''; 

        echo $phoen_product_title; ?> 

       </td> 

       <td> 

        <?php $phoen_product_sku=isset($phoen_top_products[$i]['product_sku'])?$phoen_top_products[$i]['product_sku']:'';  

         echo $phoen_product_sku; ?> 

       </td> 

       <td> 

        <?php 

         $phoen_product_quentity=isset($phoen_top_products[$i]['product_count'])?$phoen_top_products[$i]['product_count']:'';  

         echo $phoen_product_quentity; 

        ?> 

       </td> 

       <td> 
        <?php 

        $phoen_total_product_price=isset($phoen_top_products[$i]['produc_total'])?$phoen_top_products[$i]['produc_total']:''; 

         echo get_woocommerce_currency_symbol().($phoen_total_product_price); 

        ?> 
       </td> 


      </tr> 

      <?php 

      $phoen_repot_product[$i]= array(

       'name'=>$phoen_product_title, 
       'sku'=>$phoen_product_sku, 
       'product_count'=>$phoen_product_quentity, 
       'amount'=>$phoen_total_product_price 

      ); 
     } 
      $phoen_product_count++; 
    }else{ 
     ?> 
      <tr class="phoen_top_product_tr"> 
       <td> 

        <?php $phoen_product_title=isset($phoen_top_products[$i]['product_name'])?$phoen_top_products[$i]['product_name']:''; 

        echo $phoen_product_title; ?> 

       </td> 

       <td> 

        <?php $phoen_product_sku=isset($phoen_top_products[$i]['product_sku'])?$phoen_top_products[$i]['product_sku']:''; 

        echo $phoen_product_sku; ?> 

       </td> 

       <td> 

        <?php 

         $phoen_product_quentity=isset($phoen_top_products[$i]['product_count'])?$phoen_top_products[$i]['product_count']:'';  

         echo $phoen_product_quentity; 

        ?> 

       </td> 

       <td> 
        <?php 

        $phoen_total_product_price=isset($phoen_top_products[$i]['produc_total'])?$phoen_top_products[$i]['produc_total']:''; 

         echo get_woocommerce_currency_symbol().($phoen_total_product_price); 

        ?> 
       </td> 


      </tr> 

      <?php 

      $phoen_repot_product[$i]= array(

       'name'=>$phoen_product_title, 
       'sku'=>$phoen_product_sku, 
       'product_count'=>$phoen_product_quentity, 
       'amount'=>$phoen_total_product_price 

      ); 
    } 


} 

$phoen_product_file = fopen('phoen-repot-product.csv', 'w'); 

fputcsv($phoen_product_file, array('Product Name', 'SKU', 'Qty', 'Amount')); 

foreach ($phoen_repot_product as $phoen_product_row) 
{ 
    fputcsv($phoen_product_file, $phoen_product_row); 
} 

fclose($phoen_product_file);  

?> 

</tbody> 

+0

Désolé pour le retard! Je pensais avoir déjà choisi ta réponse. – Christy

Répondre

1

Dans votre requête de base de données, vous avez juste oublié d'appeler aussi le post_type comme 'product_variation'.

Vous pouvez remplacer cette ligne:

AND posts.post_type IN ('product') 

Par ceci:

AND posts.post_type IN ('product','product_variation') 

Ou cette (courte):

AND posts.post_type LIKE 'product%' 

Et vous obtiendrez également toutes les variations du produit Les données. J'ai testé votre code et cela fonctionne.
(J'ai seulement 'post_views_count' vide car je pense que c'est un champ personnalisé).

+1

Merci pour la réponse! Ajouter 'product_variation' m'a aidé à aller là où j'allais. Très appréciée! – Christy