• Hello,

    I’ve added a plugin ( WordPress content Filter ) and i’m working with Woocommerce to make a search panel. Everything is ok, except one thing :

    I’d like to order the results by date but nothing works. I’ve seen on different forums that “woocommerce_default_catalog_orderby” can be hacked but i don’t know where to find the correct way to do it.

    One more point : i had to add some extra-fields to the products, and my customer wants to sort the results by DATE ( called wccaf_date )

    Thanks a lot for your help and your answers!
    Regards
    VFERO

    https://wordpress.org/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    You’re looking to sort based on a meta field, not post date?

    https://docs.woothemes.com/document/custom-sorting-options-ascdesc/

    Thread Starter Pixel Komando

    (@vfero)

    Hello,
    Yes that’s why i’m trying to do, but i don’t know where to make these changes. Thanks for your help

    Plugin Contributor Mike Jolley

    (@mikejolley)

    The above snippets would go in theme functions.php files.

    Thread Starter Pixel Komando

    (@vfero)

    Thanks!

    Thread Starter Pixel Komando

    (@vfero)

    Is it correct this way? ( i’m nowhere in PHP, sorry… )

    <?php
    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’] = ‘asc’;//
    $args[‘meta_key’] = ‘wccaf_date’;//`

    }
    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;
    }

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Pretty much – meta key wouldn’t be needed. RAND order is a bad idea though – it will be different random products per page and you will see duplicates as you browse.

    so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?

    so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    so, is there a way to show random products, distributed over all pages. So that there wont be any duplicates?

    Not really. Also a really bad idea for caching reasons.

    You could gather all your product ID’s and randomize once. Then assign each an incremental number in a custom meta field, and sort by that.

    Could also do manual sorting for a pseudo-random appearance using the menu order meta field: https://www.sellwithwp.com/create-woocommerce-custom-product-sorting/

    this seems the solution:

    session_start();
    
    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 ) {
    
    		// Get seed from session variable if it exists
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    		}
    
    	    	// Set new seed if none exists
    	    	if ( ! $seed ) {
    	      		$seed = rand();
    	      		$_SESSION['seed'] = $seed;
    	    	}
    
    	    	// Update ORDER BY clause to use seed
    	    	$sortby = 'RAND(' . $seed . ')';
        return $sortby;
    }
Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘woocommerce_default_catalog_orderby’ is closed to new replies.