PluginUs.Net - Business Tools for WooCommerce and WordPress

[realize your idea - make your dreams come true]

Support Forum

You need to log-in to create request (topic) to the support

Edit Multi-Select Query Logic

The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page please
If you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
12

I have WPUF to allow for user-posted products. Using a metadata checkbox, they can select multiple options, which are then saved to the metadata attribute as a single string imploded using"|". In my WOOF products filter, I'm using a multi-select meta-filter to filter this attribute. However, since the attribute is saved as a string, any products with multiple selections in the attribute don't show up. I need to change my multi-select filter search logic to filter based on

if the product_meta_value equals the selection OR if the selection is anywhere in the product_meta_value

rather than only if it equals the selection. Where can I find the code regarding the multi-select filter logic decision in order to edit this logic?

Thanks!

Hello

Use  this  hook please - https://products-filter.com/hook/woof_get_meta_query/

Thank you! What code would i put within the function to make the adjustment I described? I don't know the current multi-select logic used by woof in order to be able to phrase my code. I don't know what code to type into the function hook to achieve this, but here is a more detailed explanation of the logic I need to change.

Current Logic(my guess of how it is likely structured now):

foreach($mselect_options_selected as $selection){

if( $selection== $product_attribute_value){

//add product to some array

}}

What I need:

foreach($mselect_options_selected as $selection){

if( $selection== $product_attribute_value OR in_array($selection,explode('|',product_attribute_value))){

//add product to some array

}}

 

I don't know the variable names or processes used by Woof to make this happen, so could you please explain what I should write?

Thanks!

Another option may be to leave the product meta value as a string (rather than explode()) and just search the whole string for the search selection. I'm guessing it would be something along the lines of changing the $meta_query['comparison'] to"LIKE" and the $meta_query['value'] to '%'.value.'%'

or something along those lines. I'm not sure how it would be written.

Either way is fine, but I have no idea which works best for WOOF's SQL conversion, or what I would need to write into the hook to get these changes. I also could be completely wrong about that being logic needed for the task, I'm just spitballing here. Any code you could give me to put in the hook to make the multi-select meta-filter find the various options in the product meta value's imploded array string would be greatly appreciated.

Thanks for all the help!

Hello

Everything that depends on my plugin I gave you.  This is the hook that passes the meta_query for the current search request.

Read  this -  https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

Change in current request(meta_query) to - https://c2n.me/47q6HUw.png

Okay, so basically what you're saying is that adding the filter lets me access the meta_query before it returns the final result in get_meta_query() in order to manipulate the final result? Therefore, I would need to iterate through the 2D array and where the key equals the metakey found in my metadata tab, change that array's 'compare' value to"LIKE" and not change anything with the value?

Something along the lines of

foreach($meta_query as $tmp){

if($tmp['key'] == my_meta_key){

$meta_query[$tmp]['compare'] ="Like";

}

}

I apologize for how novice of a question is, but I'm new to PHP and still trying to grasp filters. Thanks so much!

I just tried this, but it resulted in the error"There has been a critical error on your website" when I tried to reload my wpadmin portal.

//trying to make mselect query if search value is anywhere in product meta value
add_filter('woof_get_meta_query', function($meta_query) {
foreach($meta_query as &$tmp){
if($tmp['key']=='preferred_industry_types'){
$tmp['compare']="LIKE";
}
}
unset(&$tmp);
return$meta_query;
});

Just tried this one, my site works again, but it didn't change anything with the filter logic.

//trying to make mselect query if search value is anywhere in product meta value
add_filter('woof_get_meta_query', function($meta_query) {
foreach($meta_query as &$tmp){
if($tmp['key']=='preferred_industry_types'){
$meta_query[$tmp]['compare']="LIKE";
}
}
return$meta_query;
});

I was able to get it working with the following code. However, the new operation doesn't reflect in my dynamic terms count.  Is there a separate hook used for the dynamic recount query logic? Without it working in the dynamic recount, it wont let users select some options since they have a value of (0), when in reality, they have a much higher term count based on the meta query change I made, but it's only counting terms that would have been included before the filter hook was added.

 

Successful code below for anyone else that views this thread:

//trying to make mselect query if search value is anywhere in product meta value
add_filter('woof_get_meta_query', function($meta_query) {
foreach ($meta_query as $keyone => $valueone){
if(is_array($valueone)){
foreach($valueone as $keytwo => $valuetwo){
if(is_array($valuetwo)){
if($meta_query[$keyone][$keytwo]["key"]=="preferred_industry_types"){
$meta_query[$keyone][$keytwo]["compare"]="LIKE";
}}}}}
return$meta_query;
});

Hello

dynamic terms count  - it uses the same hook    -  https://products-filter.com/hook/woof_get_meta_query/

Can  you  drop  me  exact link to  the  issue?

I'll include it in the private data. I actually noticed two problems with the dynamic recount.

First, It doesn't recount based on the filters selected. The link I included has a [woof] shortcode with the product_cat tax set, and while the product_cat is set in the posts upon search, it shows counts for ALL products with that attribute value, not just that specific category. Then if you apply additional filter selections, they still don't change the counts.

Second is the problem mentioned above where the count doesn't work via the"LIKE" comparison I added in the filter for"preferred industry type", so while the query pulls up the correct posts, the count only accounts for ones with exact matches through the"=" operator.

Hello

Use  this  shortcode:

[woof taxonomies='product_cat:33' by_only='have_or_need,state,zip_code,by_text,preferred_industry_types,building_material,building_squarefeet,approximate_building_age__years_' redirect='https://sleepingdonkeys.com/real-estate-search/' ajax_redraw='1' autosubmit=1]

To  change  dynamic  recount / file - \wp-content\plugins\woocommerce-products-filter\index.php  code - https://c2n.me/47tuO7Q.png

Awesome, it worked, thanks! One thing however, the count doesn’t account for the category (since it’s a preset attribute, I guess it never updates the count since it isn’t selected to do the autosubmit), so how do I make the dynamic recount factor in the preset category without selecting it? Thanks!

Hello

the shortcode does not determine the current category, only the widget does it

You  can  use  it - https://products-filter.com/manipulate-search-data-options/

and  add  current  category.

$request['really_curr_tax'] =23-product_cat   where  23 is  ID  of category

So I have four different pages with different filters (One per category). Each filter has a different taxonomy set for product_cat. Therefore, I don't want this filter to set the same category selection for each, but instead I want each of the four filters to auto-select the category listed in the widget taxonomy attribute. Is there an if-statement or change I need to make to the code mentioned below for that to work?

 

if (is_front_page()) {
    add_filter('woof_get_request_data', 'my_woof_get_request_data');
}
function my_woof_get_request_data($request) {
   $request['really_curr_tax'] =32-product_cat;
    return $request;
}

Hello

change  it - https://c2n.me/47wRKSj.png - is_product_category()  - https://docs.woocommerce.com/document/conditional-tags/

Here  - https://clip2net.com/s/47wRUwv -  get the current category and collect text with this syntax"32-product_cat"

What do I use for an accessor to get the current category? Or do I need to use an if-else statement for like below? Lastly, the conditional tags documentation you sent said that the is_product_category() is a conditional based on if a category archive is being viewed, but it's not a category archive. It's a stand-alone page with the category attribute set, that upon submission redirects to another stand-alone page with the [woof_products] shortcode that also has the category attribute set. Will this work the same, or do I need to somehow change the page to an actual product category archive?
Here is my example code of the conditional category selection option I mentioned. Let me know which way is best, thanks!
if (is_front_page(){
    add_filter('woof_get_request_data', 'my_woof_get_request_data');
}
function my_woof_get_request_data($request{
   if ( is_product_category( 32 ) ) {
        $request['really_curr_tax'] =32-product_cat;
   }
   elseif( is_product_category( 33 ) ) {
    $request['really_curr_tax'] =32-product_cat;
   }
return $request;

}

Hello

It seems I did not understand

You have some custom pages. Each page for a category.  Right?

Why are you not comfortable using a shortcode with different attribute"taxonomy" for each page?

 

That is what I have. I thought the category archives were only referring to the specific woocommerce category pages (with the category as page title). If the custom pages work for this code, then this should work fine. However, I still don't understand how to get the current product category. to put into the syntax you showed me. Am I supposed to use conditional logic like the example I wrote, or is there a way to access the current category and include it in your described syntax?

For example:

$request['really_curr_tax'] =get_product_category().-product_cat;

 

Hello

You're right. These pages do not have current categories and you cannot define a taxonomy

You need to use different shortcodes for each page.

12