Skip Navigation

[Resolved] Sort woocommerce products by SKU

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 8 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Daniel 8 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#351107

Hi there!

I'm trying to display woocommerce products ordered by SKU using this code someona shared in this forum:

function order_by_sku_func($query, $view_settings) {
if($view_settings['view_id']!= 129) return $query;
$query['orderby'] = 'meta_value_num';
$query['order'] = 'ASC';
$query['meta_query'][] = array(
'key' => '_sku',
'value' => 0,
'compare' => '>',
);
return $query;
}
add_filter('wpv_filter_query', 'order_by_sku_func', 10, 2);

I matched view_id = 129 to my loop view id but product list order won't change.

Any suggestions?
Thanks!

#351136

Thank you for contacting us here in the Support Forum

Might I ask why you do not use the Vies GUI to sort by "_sku"?
This is easy:

1. Head to Views > Settings > Hidden custom fields

2. Click on "Edit" and check "_sku" Field

3. Save this settings

4. Head to the Products view and find "_sku" Field in your "order" settings, activate it and set your preferred order (asc/desc)

This works fine just out of the box, with no PHP code needed.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#351213

Hi Beda

I'm unable to find the "order" option anywere.
Where exactly is it located?

Thanks!

#351395
Order By.png

Find the relevant setting in my attached screenshot.

Have you successfully enabled "_sku" in the Views Settings?

Then you will find that Field now in the Order Settings of the View.

Thank you for your patience.

#351402

I see

I'm using a WordPress Archive Template to list woocommerce products. I followed this tutorial: https://toolset.com/learn/create-an-ecommerce-wordpress-site/designing-woocommerce-product-catalog/
only WordPress Archives and Content Templates are created. Those two views doesn't have Order settings.
Is there any other way to get a custom ordered list?
Thanks.

#351433

WordPress Archives Views do not have a query option because by the structure of WordPress itself, we can not manipulate the query here.

We can do that only in Views (post Views)
You can build a View in Views > Add New and query Products so to sort by and change the Query as per your needs.

This View can then be used anywhere you want.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#352003

Thank you Beda

I can't build a new View because I need products to be displayed in woocommerce's default shop page. I have some custom functions in use in this page, so I can't move products to another page.
I only need to sort them by SKU if it is possible. I tried some tutorials I found with no luck.
Is it possible in some way to order products by SKU in woocommerce's shop page using Toolset plugins or using a function?

Thanks!
Daniel

#352061

It is possible to sort Custom Post Types by Custom values (products by SKU) using Views.

If you do not want to sue Views, you need a Custom Query hooking into WordPress loop.

We can not provide this as we do not change how Plugins work, but we provide a system to adapt the output and style it, as well a Render Engine (Views) to display anything where you want on the site.

I unterstand you applied your initial Code to the ARCHIVE View?

I am not sure this will work because we can not modify the WP Archive Query.
We can only style it with Views and Our Hooks and Filters-

I suggest to create a View (Post View) and use it on the Shop (or any Other) page.

You can also use a Layout to style it more and include your View in there.

If you need this specific Sort by option, this is the only way to go.

It seems -unfortunately- that for now you need custom programming work which is beyond the scope of our support (if you do can not use Views in this case)

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#352551

Thank you Beda.

I see this is a bit more complicated than I thought it'd be. I'll try to create a post View and see if I can manage to fit it in the shop page without losing any custom modifications I've already made.

Thank you for you support.
Daniel

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.