Woocommerce shop orderby = RANDOM,如何使用分页功能防止商品在转到其他页面时重复

时间:2019-06-13 17:20:04

标签: php function woocommerce

基本功能我在函数中有一个工作代码,该代码随机显示指定类别的产品。问题是,当显示产品时,如果具有分页功能,产品将在第2/3 /页中显示重复。 我试图阻止这种情况的发生。

我曾尝试从仅能执行此操作的插件实现我的工作代码,但不针对特定类别。

因此,我试图随机分配特定类别产品的顺序。这是执行此操作的代码(但在第2/3 / ...页中有重复)

function custom_catalog_ordering_args_8( $args ) {

$product_category = 'best-sellers';

if( ! is_product_category($product_category) ) return $args;

// Set default ordering to 'date ID', so "Newness"
$args['orderby'] = 'rand';

if( $args['orderby'] == 'date ID' )
    $args['order'] = 'DESC'; // Set order by DESC

return $args;
}    

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args_8', 20, 1 );    

现在下面是该插件中的代码,该代码通过创建一小时的短暂寿命来防止产品重复 `

    //add "Random" setting to product sorting menu
    function do2_addRandomProductOrderSetting($sortby){
        $sortby['random_order'] = 'Random';
        return $sortby;
    }
    add_filter('woocommerce_default_catalog_orderby_options','do2_addRandomProductOrderSetting');
    add_filter('woocommerce_catalog_orderby','do2_addRandomProductOrderSetting');

    //randomize products when setting is used
    function do2_randomizeProductWhenSet($args){
        $orderbySetting = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) : apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
        if('random_order' == $orderbySetting){
            if(false===($seed = get_transient('do2_randSeed'))){
                $seed = rand();
                set_transient('do2_randSeed', $seed, 3600 );
            }
            $args['orderby'] = 'RAND('.$seed.')';
            $args['order'] = '';
            $args['meta_key'] = '';
        }
        return $args;
    }
    add_filter('woocommerce_get_catalog_ordering_args','do2_randomizeProductWhenSet');
}
} else {
if(!function_exists('do2_WooCommerceAdminNotice')){
    //warn on missing WooCommerce
    function do2_WooCommerceAdminNotice() {
    ?>
        <div class="notice error is-dismissible" >
            <p><?php _e('Your site must be running WooCommerce to benefit from the WooCommerce Random Product Sorting with Pagination plugin.'); ?></p>
        </div>
    <?php
    }
    add_action('admin_notices', 'do2_WooCommerceAdminNotice');
}    

以下是我尝试使其实现的效果,以使其适用于特定类别,在本例中为“畅销产品”,但它根本不起作用

 function do2_randomizeProductWhenSet($args){

$product_category = 'best-sellers';

if( ! is_product_category($product_category) ) return $args;
        $orderbySetting = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) : 

apply_filters('woocommerce_default_catalog_orderby',    

get_option('woocommerce_default_catalog_orderby'));    

if( ! is_product_category($product_category) == $orderbySetting){
            if(false===($seed = get_transient('do2_randSeed'))){
                $seed = rand();
                set_transient('do2_randSeed', $seed, 3600 );
            }
            $args['orderby'] = 'RAND('.$seed.')';
            if( $args['orderby'] == 'date ID' )
    $args['order'] = 'DESC'; // Set order by DESC
        }
        return $args;
    }

    add_filter('woocommerce_get_catalog_ordering_args','do2_randomizeProductWhenSet');    

请问有人吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助

session_start();
    $seed = $_SESSION['seed'];
    if (empty($seed)) {
      $seed = rand();
      $_SESSION['seed'] = $seed;
    }
    $sortby = 'RAND('.$seed.')';

    $atts = array(
        'orderby' => $sortby, 
        'per_page' => $per_page,//your limit products per page
        'page' => $paged, // your num page pagination
    );
    $shortcode = new WC_Shortcode_Products($atts, 'recent_products');
    echo $shortcode->get_content();