-
AuthorPosts
-
September 17, 2015 at 3:08 pm #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.
September 17, 2015 at 3:11 pm #504866Hey 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,
YigitSeptember 18, 2015 at 6:58 am #505259sorry…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,
AlexSeptember 18, 2015 at 11:43 am #505375Well…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 9 years, 2 months ago by boscotwcheung.
September 18, 2015 at 11:46 am #505378Hey!
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,
IsmaelSeptember 18, 2015 at 11:48 am #505380This 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.
September 18, 2015 at 12:24 pm #505400If 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?
September 18, 2015 at 1:28 pm #505462I 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.
September 21, 2015 at 9:35 am #506296September 21, 2015 at 9:44 am #506302I 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,
AlexSeptember 21, 2015 at 9:46 am #506303Hi!
What is the final code that you use? Might help others who want to do the same thing.
Regards,
IsmaelSeptember 21, 2015 at 9:54 am #506309add 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)
September 22, 2015 at 7:54 am #506805Hey!
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,
IsmaelSeptember 22, 2015 at 8:02 am #506808Yes, you need to add “isHot” custom field and value to all the product first. A script is needed to do a batch for this.
September 22, 2015 at 8:04 am #506809here should be what the script do,
1. get a list of products
2. call custom_product_save($post_id) for each product idSeptember 22, 2015 at 9:28 am #506845 -
AuthorPosts
- The topic ‘Product Sorting’ is closed to new replies.