Yie*_*ozi 2 wordpress product woocommerce
如何在总销售额中列出WooCommerce的所有产品?此代码仅适用于1个产品.如果在$ product上放一个数组,这是行不通的.
<?php
$product = "13";
$units_sold = get_post_meta( $product, 'total_sales', true );
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
?>
Run Code Online (Sandbox Code Playgroud)
我想输出,格式将是这样的:
[PRODUCT_NAME] - [TOTAL_SALES]
有人知道怎么做吗?谢谢.:)
为此,您可以使用带有自定义字段参数的标准get_posts()函数.如果您想让所有产品从arguments数组中删除元查询部分,下面的示例将按降序排列销售额大于零的所有帖子.结果在HTML表格中格式化.
$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>';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5619 次 |
| 最近记录: |