Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Custom field in search settings to appear dynamically
- This topic has 4 replies, 2 voices, and was last updated 5 years, 10 months ago by
Southernstarz2023.
-
AuthorPosts
-
July 28, 2020 at 8:33 pm #28743
Southernstarz2023
ParticipantHello, I have created quick screenshare video to explain my question. here is the link: https://www.loom.com/share/4d6db7aca2f941fba1aa46df30a7d630
(I am open to any php code or anything like that to distinguish if the product is in stock.)
thanks!
July 29, 2020 at 1:18 pm #28751Ernest Marcinko
KeymasterHi,
Well. not exactly but you can ignore products that are not in stock, please check this tutorial
Removing filter values is a bit more difficult, but doable programmatically via the frontend filters API.
This could be a good code skeleton to start with. All you need to implement is to get the custom field values as an array into the $exclude variable, which you want to remove from the filter, the rest should do the code itself.
add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2); function asp_change_a_filter($filters, $type) { // Enter the custom field name $field = 'custom_field_name'; // Get the custom field values to exclude from the filter $exclude = array('value1', 'value2'); foreach ($filters as $k => $filter) { if ( $filter->type() == 'custom_field' && $filter->data['field'] == $field ) { $filter->remove($exclude); } } return $filters; }July 29, 2020 at 3:42 pm #28754Southernstarz2023
ParticipantHello, thank you for the resources and the code. I just tried all of the options you gave me and it is not working. The filters are still pulling in custom fields from products that are out of stock.
This is the code I tried to implement, I added the custom field to the ‘custom field name’ but should I be doing something different? Thanks! (I also tried it with both methods from the first link you sent about showing in-stock products only and neither worked)
`add_filter(‘asp_pre_get_front_filters’, ‘asp_change_a_filter’, 10, 2);
function asp_change_a_filter($filters, $type) {
$field = ‘winery’;
$exclude = array(‘value1’, ‘value2’);foreach ($filters as $k => $filter) {
if ( $filter->type() == ‘custom_field’ && $filter->data[‘field’] == $field ) {
$filter->remove($exclude);
}
}
return $filters;
}’July 30, 2020 at 10:12 am #28757Ernest Marcinko
KeymasterI’m sorry, I may have phrased incorrectly.
The code I gave does not resolve the issue – it is only a sample code to remove values from a frontend filter, based on the custom field name.
The $exclude variable needs to be filled with the values to exclude. That section needs to be custom coded. Perhaps to do a query on the meta table with a left join to get the union of meta fields that exist in both sets. Something likeadd_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2); function asp_change_a_filter($filters, $type) { // Enter the custom field name $field = 'winery'; // Get the custom field values to exclude from the filter global $wpdb; $exclude = $wpdb->get_results(" SELECT DISTINCT(pm.meta_value) FROM $wpdb->postmeta pm LEFT JOIN $wpdb->postmeta ppm ON (pm.post_id = ppm.post_id AND ppm.meta_key = '_stock_status' AND ppm.meta_value = 'instock') WHERE pm.meta_key = '$field' ", ARRAY_N); foreach ($filters as $k => $filter) { if ( $filter->type() == 'custom_field' && $filter->data['field'] == $field ) { $filter->remove($exclude); } } return $filters; }This should be really really close to an actual solution.
July 30, 2020 at 6:06 pm #28765Southernstarz2023
ParticipantHello,
Thanks so much for your help and for the code. I will let you know if I have any other questions!
-
AuthorPosts
- You must be logged in to reply to this topic.