Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #504862

    I added the following code to the functions.php to make the default product sorting by featured product first and sort by title.

    add_filter(‘woocommerce_get_catalog_ordering_args’, ‘am_woocommerce_catalog_orderby’);
    function am_woocommerce_catalog_orderby( $args ) {
    $args[‘meta_key’] = ‘_featured’;
    $args[‘orderby’] = ‘_featured title’;
    $args[‘order’] = ‘desc’;
    return $args;

    The featured product shows first which is what I want. However, the product name is displaying from Z to A which is not I want. I want to sort the product title by alphabetical order in “ASC” after the featured sorting.

    I tried to change the $args[‘order’] to ‘desc asc’ but it doesn’t work.

    #504866

    Hey boscotwcheung!

    You can refer to this post and add order option to your elements – http://kriesi.at/documentation/enfold/how-to-add-an-orderorderby-option-to-the-blogpost-sliderportfoliomasonry-grid-element/

    Best regards,
    Yigit

    #505259

    sorry…I don’t really know how to use the function. Can this apply to product? and can I apply multiple sort? (like sort by featured desc first and title name asc second)

    Thanks,
    Alex

    #505375

    Well…I tried this before. I will show product from A to Z but the featured product will be at the end of the list. I want the featured product on top and sort from A to Z after that.

    Edited (I tried the following before):

    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $args[‘meta_key’] = ‘_featured’;
    $args[‘orderby’] = ‘meta_value title’;
    $args[‘order’] = ‘ASC’;
    return $args;
    }

    • This reply was modified 8 years, 8 months ago by boscotwcheung.
    #505378

    Hey!

    I’m sorry but I don’t think the modification will work in your case. You can actually sort the product items manually. Go to the Products panel then click “Sort Products”. Sort the product items by dragging and dropping to its respective places.

    EDIT: I deleted my first response because it’s not working. Please try the “Sort Products” feature.

    Regards,
    Ismael

    #505380

    This is a request from a customer. They have a lot product and it is hard to sort by drag and drop now. They want to hardcode the sorting.

    #505400

    If I change the featured to a custom field, can this be sorted? e.g. If I created a custom field “isFeatured” and the value is “YES” or “NO”, can I sort by this custom field and then sort from A to Z?

    #505462

    I found it can be done by the custom field. But I need to think a better custom field value for “isFeatured” as it also sort from A to Z…

    Anyway, the technical problem is solved. You can close this thread. Thanks.

    #506296

    Hey!

    How did you sort it? Mind sharing the fix? :)

    Cheers!
    Ismael

    #506302

    I changed “_featured” to a custom field.

    I did a do_action(‘wp_insert_post’, ‘xxx’). I will create a custom field called “isHot” based on the “_featured” value when saving the post.

    The logic are:
    if (“_featured” == true){ “isHot” = ‘Hot’;} else {“isHot” = ‘Normal’;}

    And now, I can sort by “isHot” and ‘title’ in ASC. The hot item will show first and then A to Z

    Thanks,
    Alex

    #506303

    Hi!

    What is the final code that you use? Might help others who want to do the same thing.

    Regards,
    Ismael

    #506309

    add this to function.php

    add_action( ‘wp_insert_post’, ‘custom_product_save’ );
    function custom_product_save($post_id){

    $post_type = get_post_type( $post_id );
    // check if post type is a product
    if ($post_type == ‘product’){
    // add isHot custom fields based on the value of “_featured”
    $isHot = get_post_meta($post_id, ‘_featured’, true);
    if ($isHot){
    update_post_meta($post_id, ‘isHot’, ‘Hot’);
    } else {
    update_post_meta($post_id, ‘isHot’, ‘Normal’);
    }
    }

    }

    // Sort the product by hot item first and A to Z after that
    add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $args[‘meta_key’] = ‘isHot’;
    $args[‘orderby’] = ‘meta_value title’;
    $args[‘order’] = ‘ASC’;
    return $args;
    }

    However, you need to update all the existing product to add the isHot custom fields. (should be easy to write a script to do a batch update)

    #506805

    Hey!

    Thanks for sharing. When I add the “custom_woocommerce_get_catalog_ordering_args” function, no products are shown in the shop page and I got this notice, “No products were found matching your selection.” Must be something on my setup. Anyway, glad you got it working on your end. Again, thanks for sharing the solution. :)

    Regards,
    Ismael

    #506808

    Yes, you need to add “isHot” custom field and value to all the product first. A script is needed to do a batch for this.

    #506809

    here should be what the script do,

    1. get a list of products
    2. call custom_product_save($post_id) for each product id

    #506845

    Hi!

    Oh. Alright. Maybe, add_post_meta will do the trick. :)

    Regards,
    Ismael

Viewing 16 posts - 1 through 16 (of 16 total)
  • The topic ‘Product Sorting’ is closed to new replies.