Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce - search by product dimensions WordPress

  • SOLVED

I am putting site together using WooCommerce and the size of each product is the vital information important to the customers.

Customers need to be able to search for products based on the length, width and depth.

WooCommerce allows you to put this information in for each product for shipping purposes and displays it on the front end.

I've tried various search plugins but none hit the mark.

As I see it, I have two options:


<strong>1. Custom Field Search</strong>

I have installed a custom field search that almost gives me what I am looking for, but it would mean the dimensions would need to be entered in the shipping fields and custom fields for every product. A lot of input duplication that I would like to avoid.

Can someone help with a script that automatically populates the custom fields with the information on the shipping fields>


<strong>2. Customised Search Facility</strong>

Develop a custom search facility that search these parameters:

- WooCommerce Length
- WooCommerce Width
- WooCommerce Height
- Custom Field or WooCommerce Attribute: Colour
- WooCommerce SKU


Hope someone can help!


Sam

Answers (1)

2014-08-06

Dbranes answers:

If you check the hidden post meta fields for each product you will find the meta keys:

_length
_width
_height
_sku
... and more ...


You might want to check out the plugin

[[LINK href="http://wordpress.org/plugins/show-hidden-post-meta/"]]http://wordpress.org/plugins/show-hidden-post-meta/[[/LINK]]

to display the hidden custom fields in the edit screen.

So you should be able to setup a normal <em>WP_Query()</em> with meta query for height, length, width and sku:

// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_height',
'value' => '10',
),
array(
'key' => '_width',
'value' => '20',
),
array(
'key' => '_length',
'value' => '30',
),
array(
'key' => '_sku',
'value' => '123',
),
)
);

// get results
$search_query = new WP_Query( $args );


This query can then be structed dynamically, depending on what search variable are set.

Did you try searching for these hidden post meta keys in your <em>Custom Field Search</em> plugin?


ps:

There's also the <em>_product_attributes</em> meta key, but it likely stores a serialized array.

For example it contains the color attribute like this:

a:1:{s:5:"color";a:6:{s:4:"name";s:5:"color";s:5:"value";s:6:"purple";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:0;}}


One idea would be to automatically save the color attribute into a custom field, when the product is edited.

<strong>Update:</strong>

Here's the current version of our plugin:
[[LINK href="https://gist.github.com/dbranes/d49c9b1ddc817fdffc3a"]]https://gist.github.com/dbranes/d49c9b1ddc817fdffc3a[[/LINK]]

[[LINK href="http://i.imgur.com/7n6c3VM.jpg"]]Here's a screenshot[[/LINK]] of the current search form, with exact or a given range (%)


Sam Cranwell comments:

Thanks : )

The hidden meta part is a revelation for me and I just tried it with my Custom Field Search plugin and it picks up the data that I wanted.

How would I go about implementing your query example in a search form of my own? This would be useful as ideally I would like the search results to be plus or minus 10% of the input length/width/height (so I can increase the returned results).

Do you have any idea how I can automatically copy attributes to custom fields? I think this would be the neatest way of searching by colour. Although I could use the <em>Types </em>plugin or similar to create a 'Colour' custom input field away from the attributes (it just rules out using the Woocommerce filtering though).


Dbranes comments:

Hi @provdes

great, I will try to provide you with some code examples later today


Dbranes comments:

Here's the first raw version:

[[LINK href="https://gist.github.com/dbranes/d49c9b1ddc817fdffc3a"]]https://gist.github.com/dbranes/d49c9b1ddc817fdffc3a[[/LINK]]

It's a plugin that you can activate and then add the shortcode [wpq_search] into a post or a page.


Dbranes comments:

I updated to version 0.0.3 with dimensional (L, H and W) search range (%) - please see the above screenshot.


Dbranes comments:

Version 0.0.4 does now support the "color" part, with automatic updates from the "color" attribute to the custom field "_wpq_color".

You can check out the latest form view here:

[[LINK href="http://i.imgur.com/7n6c3VM.jpg"]]http://i.imgur.com/7n6c3VM.jpg[[/LINK]]


Sam Cranwell comments:

Thank you ever so much for the plugin. It is definitely on the right track. I have installed it and have a few queries:

<strong>Color Search & Custom Field</strong>

- When saving a product the _wpq_color custom field is created but the value is empty. I identified the attribute name as 'pa_colour' and it has a value in the product but this isn't being transferred to the custom field.

- The current color drop down on the form has 'Yellow, Red, Green....' as options. Can this drop down pull it's values from the _wpq_color custom field?


<strong>+/- 10% Feature</strong>

- I really like the way you have included this a secondary search option. Works great. But I can see my client asking to have these drop downs removed from the form and the +/- 10% being included as a mandatory 'behind the scenes' element on all the dimension search fields. It would be great to have a version of the plugin that works this way.


<strong>Search Results</strong>

- Ideally I would like to run this plugin in a widget so I can use it in the sidebar on product pages. Is there a way open up the search results on a new page?


I really appreciate the work you are putting into this. If you want to charge me some more for your time please let me know.


Dbranes comments:

ok, I sent you PM