【问题标题】:Get the SKU in a Product list sales report在产品列表销售报告中获取 SKU
【发布时间】:2016-12-05 10:54:48
【问题描述】:

使用 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 添加到表中?

谢谢

【问题讨论】:

  • 我已经稍微更新了我的代码……有一个语法错误……让我知道它是否适合你,谢谢……

标签: php wordpress woocommerce product sku


【解决方案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-&gt;ID, "_sku", true )从wp_postmeta数据库表中获取SKU值……


或者您也可以将get_sku()方法与产品对象一起使用...

【讨论】:

  • 仅此而已?谢谢!我使用了错误的元键。
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多