wordpress WooCommerce - 显示随机产品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23440692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:31:26  来源:igfitidea点击:

WooCommerce - Show Random Products

wordpresswoocommerce

提问by Daniel Morton

I'm looking for a way to show random WooCommerce products on a page. This is nothing to do with "featured products" just random from any category.

我正在寻找一种在页面上显示随机 WooCommerce 产品的方法。这与“特色产品”无关,只是从任何类别中随机抽取。

I've been looking but cant seem to find any plugin or script to do this? Does anyone have an idea how to do this?

我一直在寻找,但似乎找不到任何插件或脚本来做到这一点?有谁知道如何做到这一点?

回答by Pec1983

Alright folks here is a bit of code I am using for mine its for recent products but is doing the job. Just add to page you want to show them on.

好吧,这里的一些代码是我用于我最近产品的代码,但正在完成这项工作。只需添加到要显示它们的页面即可。

[recent_products per_page="4" columns="4" orderby="rand" order="rand"]

[recent_products per_page="4" columns="4" orderby="rand" order="rand"]

回答by Frithir.com

Try this. Paste code into functions.php Go to wp-admin/ Woocommerce > Settings > Products > Display View settings drop down order by random will be a new option. *note: it will be the the last option.

尝试这个。将代码粘贴到 functions.php 去 wp-admin/Woocommerce > 设置 > 产品 > 显示视图设置随机下拉顺序将是一个新选项。*注意:这将是最后的选择。

// Shop random order. View settings drop down order by Woocommerce > Settings > Products > Display
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'random_list' == $orderby_value ) {
        $args['orderby'] = 'rand';
        $args['order'] = '';
        $args['meta_key'] = '';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['random_list'] = 'Random';
    return $sortby;
}

回答by Abel

This works for me:

这对我有用:

<?php
    $args = array(
        'posts_per_page'   => 1,
        'orderby'          => 'rand',
        'post_type'        => 'product' ); 

    $random_products = get_posts( $args );

    foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();
?>

回答by Jacob

This was the code I used [featured_products per_page="8" columns="4" orderby="rand"]

这是我使用的代码 [featured_products per_page="8" columns="4" orderby="rand"]

回答by Ngo Quoc Vinh

You can try it. let post it in function.php

你可以试试看。让它在function.php中发布

 add_filter('woocommerce_get_catalog_ordering_args', 'set_sort_order');
   function set_sort_order($args) {
     $args['orderby'] = 'rand';
     return ($args);    
   }