【问题标题】:Listing WooCommerce best selling products without a shortcode列出没有短代码的 WooCommerce 畅销产品
【发布时间】:2019-06-13 10:23:37
【问题描述】:

所以,我一直在尝试做与 [products limit="10" columns="4" best_selling="true" ] 短代码完全相同的事情。

我需要将它实现到一个 php 页面模板,但我读过 do_shortcode 是不好的做法,我想以正确的方式来做。

这就是我目前所拥有的,但我该去哪里呢?

$args = array(
        'limit' => '10',
        'columns'      => '4',
        'orderby' => 'total_sales',
        'order' => 'DESC',
);
$products = wc_get_products( $args );

如果我理解正确,这将保存 10 个最畅销的产品,按总销售额递减为 $products。这不正确吗?

如何正确显示 $products 中的实际产品列表(就像短代码一样)?

【问题讨论】:

    标签: php wordpress woocommerce product


    【解决方案1】:

    total_sales 可以使用meta_key 在查询中设置。

    从查询返回结果后,您只需遍历它们并输出您需要的任何属性。

    $args = array(
            'limit'     => '10',
            'orderby'   => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ),
            'meta_key'  => 'total_sales',
        );
    
        $query    = new WC_Product_Query( $args );
        $products = $query->get_products();
        if ( $products ) {
            foreach ( $products as $product ) {
                echo $product->get_name(); // Here, we're just listing each product's name
            }
        } else {
            echo __( 'No products found' );
        }
    

    更新

    通过此更新,我们现在将来自wc_get_products() 的结果用于改编自archive-product.phpcustom page template。这里的目标是避免使用 WP_Query/get_posts(),因为它们是 not recommended for product querying

    wc_get_products 和 WC_Product_Query 提供了一种检索产品的标准方法,该方法可以安全使用并且不会因未来 WooCommerce 版本中的数据库更改而中断。随着数据向自定义表移动以获得更好的性能,构建自定义 WP_Queries 或数据库查询可能会破坏您在未来版本的 WooCommerce 中的代码。这是插件和主题开发人员检索多个产品的最佳实践方式。 wc_get_products 和 WC_Product_Query 类似于 WordPress get_posts 和 WP_Query。就像那些一样,你传入一个定义搜索条件的参数数组。

    我们现在能够获得与普通产品类别/存档页面相同的布局/样式,但使用我们的畅销产品查询。我们已经应用了产品标题、图片、价格和添加到购物车按钮以及所有 WooCommerce/主题样式,而无需像之前的方法(上图)那样从头开始构建所有内容。

    在 WooCommerce 3.5.6 中测试和工作

    defined( 'ABSPATH' ) || exit;
    get_header( 'shop' );
    do_action( 'woocommerce_before_main_content' );
    ?>
        <header class="woocommerce-products-header">
            <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
                <h1 class="woocommerce-products-header__title page-title"><?php echo get_the_title(); ?></h1>
            <?php endif; ?>
        </header>
    <?php
    
    
    if ( ! function_exists( 'wc_get_products' ) ) {
        return;
    }
    echo '<div class="woocommerce">'; // needed for default styles 
    $top_selling_products = wc_get_products( array(
        'meta_key' => 'total_sales', // our custom query meta_key
        'return'   => 'ids', // needed to pass to $post_object
        'orderby'  => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ), // order from highest to lowest of top sellers
    ) );
    if ( $top_selling_products ) {
        do_action( 'woocommerce_before_shop_loop' );
        woocommerce_product_loop_start();
        foreach ( $top_selling_products as $top_selling_product ) {
            $post_object = get_post( $top_selling_product );
            setup_postdata( $GLOBALS['post'] =& $post_object );
            do_action( 'woocommerce_shop_loop' );
            wc_get_template_part( 'content', 'product' );
        }
        wp_reset_postdata();
        woocommerce_product_loop_end();
        do_action( 'woocommerce_after_shop_loop' );
    } else {
        do_action( 'woocommerce_no_products_found' );
    }
    echo '</div><!-- .woocommerce -->';
    
    do_action( 'woocommerce_after_main_content' );
    do_action( 'woocommerce_sidebar' );
    get_footer( 'shop' );
    

    【讨论】:

    • 谢谢! WooCommerce 文档不是说不应该使用 WP_Query 而应该使用 wc_get_products 或 WC_Product_Query 吗?还是我误会了?
    • 天哪,这真是漫长的一天。对于那个很抱歉。更新了!
    • 谢谢!有没有办法像简码一样获得“整个列表”?使用产品图片、产品名称、价格、添加到购物车按钮?我检查了文档,但不幸的是我找不到相关部分
    • 更新了包含产品名称、图片、价格和添加到购物车按钮的方法。
    • 哇,谢谢!完美运行!也感谢您的解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2016-09-05
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多