• Sami S

    (@sami-sanpakkila)


    Hi!

    I have a code in my online shop that creates a dropdown to order Artists in alphabetical order from A-Z and Z-A.

    However it’s broken at some point during an update. So I’m looking for help on how to fix it. Below is the code I’m using and link to the page.

    The funny thing is that the code works from shop page 2 onwards! Page one (and two products on page two) are the products that don’t align with the alphabetical order. So if you choose drop down Artists Z-A the first three pages are correctly in alphabetical order. I’ve also tried a few other similar codes and the problem is the same.

    Any advice would be much appreaciated!

    /**
     * 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 'esittaja_asc':
    			$sort_args['orderby']  = 'meta_value';
    			// Sort by meta_value because we're using alphabetic sorting
    			$sort_args['order']    = 'asc';
    			$sort_args['meta_key'] = 'pa_esittaja';
    			// use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
    			break;
    			case 'esittaja_desc':
    			$sort_args['orderby']  = 'meta_value';
    			// Sort by meta_value because we're using alphabetic sorting
    			$sort_args['order']    = 'desc';
    			$sort_args['meta_key'] = 'pa_esittaja';
    			// 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['esittaja_asc'] = __( 'ARTIST A-Z', 'woocommerce' );
    		$sortby['esittaja_desc'] = __( 'ARTIST Z-A', '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 topic was modified 4 years, 7 months ago by Sami S.
    • This topic was modified 4 years, 7 months ago by Sami S.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter Sami S

    (@sami-sanpakkila)

    As a side note the order does not correctly alphabetise on the All Products admin page either. If I sort by date, brand, keyphrase etc. it does so correctly without any problems.

    • This reply was modified 4 years, 7 months ago by Sami S.
Viewing 1 replies (of 1 total)
  • The topic ‘Custom alphabetical artist order code not working’ is closed to new replies.