• Hi,

    In my page : https://cappiello.t4b.mx/index.php/catalogue/

    I want to default order by SKU asc ( correspond to “trier par date” as first 8 chars of SKU is date )

    I tried to do it :

    add_filter(‘woocommerce_get_catalog_ordering_args’, ‘am_woocommerce_catalog_orderby’);
    function am_woocommerce_catalog_orderby( ) {
    $args[‘meta_key’] = ‘_sku’;
    $args[‘orderby’] = ‘meta_value’;
    $args[‘order’] = ‘asc’;
    return $args;
    }

    or like that:

    add_filter(‘woocommerce_default_catalog_orderby’, ‘custom_default_catalog_orderby’);

    function custom_default_catalog_orderby() {
    return ‘_sku’; // Can also use title and price
    }

    In my theme child functions.php,

    but none works.

    What am I missing???

Viewing 8 replies - 1 through 8 (of 8 total)
  • Please don’t go through all of that trouble.
    There is a plugin called Booster for WooCommerce, it literally does exactly that and even more.
    It has the direct option for sorting sku by asc or desc order.
    I wouldn’t do that work when someone has already done it for you.

    • This reply was modified 7 years, 11 months ago by Joey - a11n.

    WooCommerce –> Booster Settings –> Products –> Sorting –> (check) [ ] Enable Module (under sorting)

    Thread Starter xoco

    (@xoco)

    Nice plugin!!!

    But it doesn’t work ?? I also tried with several plugin doing the same thing and no result…

    My page : https://cappiello.t4b.mx/index.php/catalogue/

    My theme : Avada

    I don’t really understand why this is happening

    Plugin Support RK a11n

    (@riaanknoetze)

    If I recall correctly, Avada has WooCommerce template overrides in place which might be affecting product sorting. Test your code (or the plugin above) when using just the default Twenty Sixteen theme ??

    Thread Starter xoco

    (@xoco)

    Yes, this is exactly the problem. I tried with Twenty Sixteen theme and it works.

    Avada support is not helping much when talking code…. Do you have an idea where I should search???

    Your first code worked for me too. I wouldn’t expect your second code to work.

    One reason why your code may not be working in Avada is that Avada may already have that filter, and theirs is taking priority over yours. I don’t have Avada to check. Can you text search Avada files for this filter? If not, just try a high number for the priority parameter. High numbers execute later so take precedence.
    add_filter(‘woocommerce_get_catalog_ordering_args’, ‘am_woocommerce_catalog_orderby’, 99);

    Thread Starter xoco

    (@xoco)

    I changed it, but still not working ??

    When searching for Avada code, I get this :

    function avada_woocommerce_ordering( $query ) {
    	// We only want to affect the main query.
    	if ( ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( absint( $query->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) || $query->is_post_type_archive( 'product' ) || $query->is_tax( get_object_taxonomies( 'product' ) ) ) {
    		if ( Avada()->settings->get( 'woocommerce_avada_ordering' ) ) {
    			remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
    			add_action( 'woocommerce_before_shop_loop', 'avada_woocommerce_catalog_ordering', 30 );
    
    			add_action( 'woocommerce_get_catalog_ordering_args', 'avada_woocommerce_get_catalog_ordering_args', 20 );
    		}
    	}
    }

    And also :

    public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
    		// Get ordering from query string unless defined
    		if ( ! $orderby ) {
    			$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    
    			// Get order + orderby args from string
    			$orderby_value = explode( '-', $orderby_value );
    			$orderby       = esc_attr( $orderby_value[0] );
    			$order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
    		}
    
    		$orderby = strtolower( $orderby );
    		$order   = strtoupper( $order );
    		$args    = array();
    
    		// default - menu_order
    		$args['orderby']  = 'menu_order title';
    		$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    		$args['meta_key'] = '';
    
    		switch ( $orderby ) {
    			case 'rand' :
    				$args['orderby']  = 'rand';
    			break;
    			case 'date' :
    				$args['orderby']  = 'date ID';
    				$args['order']    = $order == 'ASC' ? 'ASC' : 'DESC';
    			break;
    			case 'price' :
    				$args['orderby']  = "meta_value_num ID";
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    				$args['meta_key'] = '_price';
    			break;
    			case 'popularity' :
    				$args['meta_key'] = 'total_sales';
    
    				// Sorting handled later though a hook
    				add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
    			break;
    			case 'rating' :
    				// Sorting handled later though a hook
    				add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
    			break;
    			case 'title' :
    				$args['orderby']  = 'title';
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    			break;
    		}
    
    		return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
    	}

    i came into contact with the devs from Avada and their answer was (to resume it) “Here is a link to a site so that you will need to pay 100-200$+ for it to be done.”

    I had found the code to override it, but when bringing it up this is what i got: “This was used when the post was made it is now removed from the code and is now useless.” the code is still in the files but it is not used.

    Now it is in the includes files that can’t be override by the child theme. You will need to unhook the one from Avada before you can add your own. but the hard part is what to unhook as i am still searching for the hook in question.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Default sort by SKU’ is closed to new replies.