Ask your WordPress questions! Pay money and get answers fast! (more info)

WooCommerce custom Catelog ordering WordPress

Hi there, please can someone amend the below Product Sort function so the output is Featured Products first, followed by all non-featured products. Both Featured and Non featured products should be ordered by title, alphabetically.

My attempt below throws featured products to the bottom of the loop, whereas I'd like featured products to always be the first items in my loop.

Thank you.


// http://docs.woothemes.com/document/custom-sorting-options-ascdesc/
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 ( 'featured_first' == $orderby_value ) {
$args['meta_key'] = '_featured';
$args['orderby'] = 'meta_value title';
$args['order'] = 'ASC';
$args['paged'] = $paged;
}
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['featured_first'] = 'Featured First';
return $sortby;
}

Answers (2)

2013-07-19

Arnav Joy answers:

i am not sure if it will work or not , but try

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 ( 'featured_first' == $orderby_value ) {

$args['meta_key'] = '_featured';

$args['orderby'] = 'meta_value title';

$args['order'] = 'DESC';

$args['paged'] = $paged;

}

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['featured_first'] = 'Featured First';

return $sortby;

}


designbuildtest comments:

Hi Arnav, no doesn't work sorry. This simply reverses the alpha order. Featured posts are still displayed at the end of the loop, not at the beginning.

2013-07-19

Sabby Sam answers:

Hi,
I will suggest you to try this code ( visit the url http://docs.woothemes.com/wc-apidocs/source-class-WC_Query.html#351-412 )

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

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 ( 'featured_first' == $orderby_value ) {

$args['meta_key'] = '_featured';

$args['orderby'] = 'title'; /* ( I have changed this one ) */

$args['order'] = 'ASC';

$args['paged'] = $paged;

$args['value'] ='yes'; /* ( I have added this one ) */


}

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['featured_first'] = 'Featured First';

return $sortby;

}


designbuildtest comments:

Hi Sabby, your suggestion orders products alphabetically only ... featured products do not appear first in the loop unfortunately.


Sabby Sam comments:

Hmmmm, Did you tried this one

$args['orderby'] = 'meta_value title';



$args['order'] = 'DESC';


designbuildtest comments:

Hi. Yes, I've tried this also - but not the desired result unfortunately.