• Resolved Morphim

    (@morphim)


    Hello

    Is there a function I can use to sort the products by a different word in the title other than the first?

    My products have names & I’d like to sort but surname (which is second) rather than the first name, which is obviously the first word.

    For example – current alphabetical sort:
    Annie Lennox – Diva
    Bruce Springsteen – Born in the USA
    David Bowie – Hunky Dorey

    How I’d like it – sort by second word:
    David Bowie – Hunky Dorey
    Annie Lennox – Diva
    Bruce Springsteen – Born in the USA

    It would be really great if this is possible.
    Thanks in advance.

Viewing 10 replies - 1 through 10 (of 10 total)
  • madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @morphim,

    WooCommerce won’t do that automatically, but there’s a Sorting link on the WP Admin > Products page that will let you drag and drop the product into the correct order:

    Screenshot: https://cld.wthms.co/OVODFN

    If all the products don’t fit on one page, you can use the Screen Options menu in the top right corner to increase the number of products showing there:

    Screenshot: https://cld.wthms.co/fVCvUE

    Cheers!

    Thread Starter Morphim

    (@morphim)

    Thanks for the reply. I understand all of the built in sorting options but doing it by hand isn’t practical.
    At the moment, I’ve done it (rather labouriously) but sorting products by hand, into the alphabetical order I need, on a spreadsheet then using ‘menu order’ which works but needs amending, by hand, whenever new products are added.

    I’ve found these pieces of code to enable a similar thing with regular WP post titles. Is it possible to amend something like this for woocommerce products?

    function posts_orderby_lastname ($orderby_statement)
    {
    $orderby_statement = “RIGHT(post_title, LOCATE(‘ ‘, REVERSE(post_title)) – 1) ASC”;
    return $orderby_statement;
    }

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Even though sorting in WooCommerce works in a different way, I have another idea. What if you added author’s last name in a custom meta field? You can add one in the Custom Fields section on the product page:

    https://cld.wthms.co/hje3o

    Once you do that, you could sort all products by that custom field in alphabetic order. Here is a code example that can help you get started:

    https://gist.github.com/bekarice/0df2b2d54d6ac8076f84

    Does this look like something that could work for you?

    Thread Starter Morphim

    (@morphim)

    Hello.

    Thanks very much for this. I’ve tried to implement what you’ve suggested but I can’t get it to work. I’m no coder so have to fly by the seat of my pants : )

    This is what I tried:
    Created a new custom field via the products page called ‘surname’

    Then placed this code in my child theme functions:

    /**
     * Adds WooCommerce catalog sorting options using postmeta, such as custom fields
     * Tutorial: https://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/
    **/
    function skyverge_add_postmeta_ordering_args( $sort_args ) {
    		
    	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    	switch( $orderby_value ) {
    	
    		// Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
    		case 'location':
    			$sort_args['orderby']  = 'meta_value';
    			// Sort by meta_value because we're using alphabetic sorting
    			$sort_args['order']    = 'asc';
    			$sort_args['meta_key'] = 'surname';
    			// use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
    			break;	
    	}
    	
    	return $sort_args;
    }
    add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );
    // Add these new sorting arguments to the sortby options on the frontend
    function skyverge_add_new_postmeta_orderby( $sortby ) {
    	
    	// Adjust the text as desired
    	$sortby['surname'] = __( 'Sort by surname', 'woocommerce' );
        
    	return $sortby;
    }
    add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );
    
    /**
     * This code should be added to functions.php of your theme
     **/
    add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby'); function am_woocommerce_catalog_orderby( $args ) { $args['meta_key'] = 'surname'; $args['orderby'] = 'meta_value'; $args['order'] = 'asc'; return $args; }

    The second part is to try and get this method of sorting to work by default since I’ve removed the ‘sort by’ dropdown on the products page as this is the only sorting I need to display.

    Thanks very much in advance if you are able to point me in the right direction.

    • This reply was modified 7 years, 1 month ago by Morphim.
    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @morphim,

    The code you’ve sent looks correct to me. As the next step, we’ll need to add a field called surname in the Custom Fields section of all products:

    Screenshot: https://cld.wthms.co/agHBTU

    Once you do that, you can go to Customizer > WooCommerce > Product Catalog and change Default product sorting to Sort by surname. Here is how my test products are ordered before setting this:

    Screenshot: https://cld.wthms.co/es0ifs

    And here is after:

    Screenshot: https://cld.wthms.co/lFWUgZ

    Cheers!

    Thread Starter Morphim

    (@morphim)

    Hello again.
    Thanks very much for taking the time to help me on this. I appreciate it : )

    I set up one product with ‘surname’ custom field but is there a way to add this field automatically to all products rather than having to edit / add to each product?
    I understand that I need to input the actual second name into all products but at the moment I have to recreate the ‘surname’ custom field individually in each product.

    Thank again.

    Thread Starter Morphim

    (@morphim)

    Hello again. Don’t worry about the above. I added everyone’s surname via the ‘surname’ meta field on my spreadsheet which created the ‘surname’ custom field within each product.

    However, having done this and reset all of the position / menu order ids to zero (this was how I had been sorting alphabetically) I still can’t get it to work like your screenshots.
    Did you input my code exactly into the theme function file then set the sort order to surname and it just worked?
    It’s still sorting alphabetically by first word / name. Very frustrating.

    Any ideas? Thanks in advance (again : )

    Thread Starter Morphim

    (@morphim)

    Back again. I got it working. My code had an error:

    // Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
    		case 'location':

    I missed the ‘location’ text which needed changing to ‘surname’
    Once done, it worked : )
    However, this works with standard Woo product list but I’m using Woof filter which doesn’t honour the ‘surname’ sorting.
    I’ll contact them to try to find a fix (unless you can suggest anything??)

    If not, thanks again for all your help.

    Thread Starter Morphim

    (@morphim)

    Hello. Last message, I promise : )

    I got this all working. Thanks for your help.

    The guys at WOOF filter sent me this:
    add code to:
    plugins\woocommerce-products-filter\index.php”

    case ‘surname’ :
    $meta_key = ‘surname’;
    $order = ‘ASC’;
    break;

    Which got their plugin to honour the ‘surname’ meta orderby.

    I’m actually thinking now though that using the second word in the title (surname) as the sku then sorting with that would be easier but can’t work out how to do it.

    I’m sure I’ll get there.

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @morphim,

    Glad to hear you’ve got this working for you.

    I’m going to mark this as resolved now.

    If you have any further questions, you can start a new thread.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Sort products by last word in title?’ is closed to new replies.