Skip Navigation

[Resolved] Custom Search 'Between' WooCommerce Attributes

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 7 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#443098

Hi, I am trying to create a search page for a client that is using WooCommerce. The Global Product Attributes are numeric and we need to have the ability to search for 'between' these values. For example, 'height' in our use is a WooCommerce Attribute ('pa_height') and we need to search between values 0-200cm. From what I can see, this is possible for custom meta fields using the 'Text Field' and 'Between' function, but how can we do this for the WC Attributes?

Thanks 🙂

#443653

Hi, just wondering why I haven't had a reply to this yet?

Thanks

#443659

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Steven

Sorry for the delay in responding to you, we are currently experiencing a high volume of support requests.

It isn't possible to add a product attribute as a search filter. They are stored as a hidden field in the post meta table, but the problem is that all attributes are stored in an array as a serialized string. To be usable as a search parameter they would need to be stored as key value pair, e.g. height 200.

You can get this to work, but it requires a certain duplication of effort.

Create a custom field group for product attributes, and add custom fields for each attribute you require (e.g a numeric field for height).

Then on the edit product pages you can copy the attribute values into the custom fields. Those custom fields will then be available as filters in your custom product search.

If you know some PHP you could automate this to some extent so that when the client updates the product attribute the custom field is automatically updated in the database. A quick perusal of the WooCommerce API documentation shows they have hooks for woocommerce_attribute_added, woocommerce_attribute_updated, and woocommerce_attribute_deleted (https://docs.woocommerce.com/wc-apidocs/hook-docs.html).

#444831

Hi Nigel,

Thanks for getting back to me. This may create a slight issue. The products get populated via the WooCommerce RestAPI from an external app called Podio - these get entered into the site automatically as Attributes. The Podio system then automatically sends through changes to the website of these values every so often.

If we ran with the process you mention, I suppose we would have to manually go and change these values when they got updated via the API?

If not, then do these need to be custom meta fields? I would instead have to look at getting the system re-customised to save the data as meta fields in which case.

Thanks 🙂

#445080

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Steve

I would expect the WooCommerce hooks I described to be triggered no matter how the attributes are updated (unless WordPress were being bypassed completely and the values updated in the database directly, but if you are going via the WC REST API then that's not the case). So you could use them and respond to those actions as if a user were manually entering the attribute values in the edit product screen and saving.

There is, I think, no alternative to having the attributes as Types custom fields if you want to be able to filter or search by them in Views, so that they are stored as simple key : value pairs as strings and not as the serialised arrays used by WC.

Depending on your needs you may want to re-configure and use exclusively Types custom fields for them, or you may want to go down the duplication route (depending on whether WC needs to handle the attributes in any way, even if it is just displaying them in the relevant tab if you are using the standard product page layout).

Keep me posted if you run into any difficulties.

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