如何在 WordPress WooCommerce 中使用短代码在特定页面中显示最近出售的商品? [英] How show recently sold items in a specific page using shortcode in WordPress WooCommerce?

查看:27
本文介绍了如何在 WordPress WooCommerce 中使用短代码在特定页面中显示最近出售的商品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于数字产品的 WooCommerce 网站.销售的产品通常有一个名为最近销售"的自定义标签.但是,我想在仅显示已售商品的特定页面中显示所有已售商品.

I have a WooCommerce website for digital products. The products that sold generally have a custom tag named "Recently Sold". But, I want to show all the sold items in a specific page where only the sold items will be displayed.

我有一个插件WooCommerce Toolkit",它在侧边栏中显示最近出售的商品,另一个是Woo 最近出售的商品列表",它仅在单个产品页面中显示最近出售的商品.

I got a plugin "WooCommerce Toolkit" Which show recently sold items in the sidebar and another is "Woo recently sold items list" which show recently sold item only in the single product page.

我想在已售"页面等页面上显示所有已售出的商品.

I want to show all the sold items on a page like "Sold" page.

我希望它现在有意义.

推荐答案

add_shortcode( 'sold_products', 'wc_products_allsold' );

function wc_products_allsold() {

    //  Get last week orders
    $all_orders = wc_get_orders(
    array(
        'limit'  => -1,
        'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
        'return' => 'ids',
    )
    );

    // Sum quantities purchased

    $count               = 0;
    $produt_sale_list    = array();
    foreach ( $all_orders as $all_order ) {
        $order   = wc_get_order( $all_order );
        $items   = $order->get_items();
        foreach ( $items as $item ) {

            $produt_sale_list[ $item->get_name() ][]     = $item[ 'qty' ];
            $count                                   = $count + absint( $item[ 'qty' ] );
        }
    }

    foreach ( $produt_sale_list as $produt_sold => $value ) {
        echo "<p>$produt_sold sold: " . array_sum( $value ) . "</p>";
    }
    if ( $count > 0 )
        echo "<p>Recent sales: $count</p>";
}

将短代码 [sold_products] 添加到您要显示所有已售产品的页面中.

Add the shortcode [sold_products] into the page where you want to display all sold products.

这篇关于如何在 WordPress WooCommerce 中使用短代码在特定页面中显示最近出售的商品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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