2016-12-05 54 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

那所有?谢谢!我使用了错误的元键。 –