2016-12-05 53 views
3

使用WooCommerce,我有這樣的代碼,輸出產品列表報告:獲取SKU的產品列表銷售報告

$args = array(
    'post_type' => 'product', 
    'posts_per_page' => -1, 
    'meta_key' => 'total_sales', 
    'orderby' => 'meta_value_num', 
    'order' => 'DESC', 
    'meta_query' => array(
     array(
      'key' => 'total_sales', 
      'value' => 0, 
      'compare' => '>' 
     ) 
    ) 
); 
$output = array_reduce(get_posts($args), function($result, $post) { 
    return $result .= '<tr><td>' . $post->post_title . '</td><td>' . get_post_meta($post->ID, 'total_sales', true) . '</td></tr>'; 
}); 
echo '<table><thead><tr><th>' . __('Product', 'woocommerce') . '</th><th>' . __('Units Sold', 'woocommerce') . '</th></tr></thead>' . $output . '</table>'; 

與該代碼我想列出一個WordPress頁面上的銷售。

我的問題:如何將SKU添加到表格中?

感謝

+0

我已經輕輕地更新我的代碼......有一個語法錯誤...讓我知道它是否適合你的感謝... – LoicTheAztec

回答

1

- 光更新 -

您可以添加SKU改變一點點你的代碼是這樣的:

$args = array(
    'post_type' => 'product', 
    'posts_per_page' => -1, 
    'meta_key' => 'total_sales', 
    'orderby' => 'meta_value_num', 
    'order' => 'DESC', 
    'meta_query' => array(
     array(
      'key' => 'total_sales', 
      'value' => 0, 
      'compare' => '>' 
     ) 
    ) 
); 

$output = array_reduce(get_posts($args), function($result, $post) { 
    return $result .= ' 
    <tbody> 
     <tr> 
      <td>' . $post->post_title . '</td> 
      <td>' . get_post_meta($post->ID, "total_sales", true) .'</td> 
      <td>' . get_post_meta($post->ID, "_sku", true) .'</td> 
     </tr> 
    </tbody>'; 
}); 

echo '<table> 
    <thead> 
     <tr> 
      <th>' . __("Product", "woocommerce") . '</th> 
      <th>' . __("Units Sold", "woocommerce") . '</th> 
      <th>' . __("Sku", "woocommerce") . '</th> 
     </tr> 
    </thead>' . $output . ' 
</table>'; 

我在這裏使用get_post_meta($post->ID, "_sku", true)從wp_postmeta數據庫中獲取SKU值表...


或者你也可以用產品使用對象的方法get_sku() ...

+0

那所有?謝謝!我使用了錯誤的元鍵。 –