如何列出总销售额的所有产品? [英] How to list all products with total sales?

查看:83
本文介绍了如何列出总销售额的所有产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何列出来自 WooCommerce 的所有产品以及总销售额?此代码仅适用于 1 个产品.如果在 $product 上放置一个数组,则不起作用.

我想要输出,格式是这样的:

[PRODUCT_NAME] - [TOTAL_SALES]

解决方案

试试这个代码.它将以 2 种格式为您提供输出

get_results("SELECT p.post_title 作为产品,pm.meta_value 作为 total_sales FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON (p.ID = pm.post_id AND pm.meta_key LIKE 'total_sales') WHERE p.post_type LIKE 'product' AND p.post_status LIKE 'publish'", 'ARRAY_A');?><表格><tr><th><?php _e('产品');?></th><th><?php _e( '销售单位');?></th></tr><?phpforeach ( $results as $result ) {echo "";回声<td>".$result['product'] ."</td>";回声<td>".$result['total_sales'] ."</td>";回声</tr>";}?><div><p><strong><?php echo __( 'Product' ) .' - ' .__( '单位已售' );?></strong></p><?phpforeach ( $results as $result ) {回声<p>".$result['product'] .' - ' .$result['total_sales'] ."</p>";}?>

希望对你有帮助

How to list all products from WooCommerce with total sales? This code is just for 1 product only. It's not work if put an array on $product.

<?php
$product = "13";
$units_sold = get_post_meta( $product, 'total_sales', true );
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
?>

I want on output, format will be like this:

[PRODUCT_NAME] - [TOTAL_SALES]

解决方案

Try this code. It'll give you output in 2 format

<?php
global $wpdb;
$results = $wpdb->get_results("SELECT p.post_title as product, pm.meta_value as total_sales FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON (p.ID = pm.post_id AND pm.meta_key LIKE 'total_sales') WHERE p.post_type LIKE 'product' AND p.post_status LIKE 'publish'", 'ARRAY_A');
?>
<table>
    <tr>
        <th><?php _e( 'Product' ); ?></th>
        <th><?php _e( 'Unit sold' ); ?></th>
    </tr>
<?php
foreach ( $results as $result ) {
    echo "<tr>";
    echo "<td>" . $result['product'] . "</td>";
    echo "<td>" . $result['total_sales'] . "</td>";
    echo "</tr>";
}
?>
</table>
<div>
    <p><strong><?php echo __( 'Product' ) . ' - ' . __( 'Unit Sold' ); ?></strong></p>
    <?php
    foreach ( $results as $result ) {
        echo "<p>" . $result['product'] . ' - ' . $result['total_sales'] . "</p>";
    }
    ?>
</div>

Hope this will be helpful

这篇关于如何列出总销售额的所有产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆