• Resolved jasonlpeck

    (@jasonlpeck)


    I’m trying to add a custom sort by option to the drop down when viewing products. I’ve added the following code, but when the option is selected, I’m routed to https://new.tmvavra.com/search/?orderby=test and I get a No Products Found message. I suspect I’m not using the correct meta-key value but can’t seem to get it to work. What am I missing?

    function custom_product_sort( $sort_args ) {
    // Get the selected order by value
    $orderby_value = isset( $_GET[‘orderby’] ) ? woocommerce_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );

    switch ($orderby_value) {
    case ‘alphabetical’ :
    $sort_args[‘order’] = ‘ASC’;
    $sort_args[‘meta_key’] = ”;
    $sort_args[‘orderby’] = ‘title’;
    break;
    case ‘test’ :
    $sort_args[‘post_type’] = ‘product’;
    $sort_args[‘order’] = ‘ASC’;
    $sort_args[‘meta_key’] = ‘bathrooms’;
    $sort_args[‘orderby’] = ‘meta_value_num’;
    break;
    }

    return $sort_args;
    }
    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_product_sort’ );

    function add_custom_product_sort_options( $sortby ) {
    $sortby[‘alphabetical’] = ‘Sort by name’;
    $sortby[‘test’] = __( ‘Sort by Test’, ‘textdomain’ );
    return $sortby;
    }
    add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘add_custom_product_sort_options’ );
    add_filter( ‘woocommerce_catalog_orderby’, ‘add_custom_product_sort_options’ );

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • “bathrooms” looks like a category, not a meta key. Its not possible to sort by category, since products may be in multiple categories.

    To find available meta keys, go to phpMyAdmin and look at the postmeta table. Pick a product ID.

    Ones you might use include:
    _price
    _regular_price
    _sale_price
    _weight
    _length
    _width
    _height
    _sku
    _stock
    _download_limit

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    Hi @jasonlpeck,

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    Thanks, @lorro for the assist.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Sort By’ is closed to new replies.