Home:ALL Converter>WooCommerce custom random vendor product sorting in shop archive

WooCommerce custom random vendor product sorting in shop archive

Ask Time:2022-05-18T06:29:03         Author:Nik7

Json Formatter

I'm currently working on a custom random product sorting based on vendors from Dokan Plugin in the WooCommerce product archive loop. We want that the systems displays the product from the vendors randomly but all product from one vendor always together.

Example: [Product 1 Vendor A], [Product 2 Vendor A], [Product 3 Vendor A], [Product 3 Vendor C], [Product 1 Vendor C], [Product 2 Vendor C], [Product 2 Vendor D], [Product 1 Vendor D], [Product 3 Vendor D]

  • Goal 1: Group all products on vendor base together (the vendor order should be random)
  • Goal 2: One specific vendor should be always at the top of the sorting on the first page.

For that I created the following snipped: But it does not work and I have no idea what's wrong here.

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' ) );

        // Get all vendors
        global $product;
        $seller = get_post_field( 'post_author', $product->get_id());
        $author  = get_user_by( 'id', $seller );
        $vendor = dokan()->vendor->get( $seller );  
    
        // One specific Vendor always at the top (Vendor ID =  57)
        
              // do some magic here...
        
        // Random function
        if ( 'random_list' == $orderby_value ) {
        $args['orderby'] = $vendor;
        $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 custom';

    return $sortby;
}                       

Author:Nik7,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72281346/woocommerce-custom-random-vendor-product-sorting-in-shop-archive
yy