使用WooCommerce,我有这个代码输出产品列表报告:
$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 :(得分: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()
...