Last active
April 9, 2024 06:55
-
-
Save sachyya/f4c2c34ca5c8900880f23c06cf0dc8e1 to your computer and use it in GitHub Desktop.
Search based on product catalog visibility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add to schema | |
add_filter( 'cm_tsfwc_product_fields', 'your_slug_add_stock_status_to_schema' ); | |
function your_slug_add_stock_status_to_schema( $product_fields ) { | |
$product_fields[] = [ 'name' => 'catalog_visibility', 'type' => 'string', 'facet' => true ]; | |
return $product_fields; | |
} | |
// Format data for added schema | |
add_filter( 'cm_tsfwc_data_before_entry', 'your_slug_add_data_before_entry', 10, 4 ); | |
function your_slug_add_data_before_entry( $formatted_data, $raw_data, $object_id, $schema_name ) { | |
if( $schema_name === 'product' ) { // only add data if the schema is product | |
$formatted_data['catalog_visibility'] = $raw_data->get_catalog_visibility(); | |
} | |
return $formatted_data; | |
} | |
// Use filter_by to filter data based on the catalog visibility | |
// On autocomplete bar | |
add_action( 'cm_tsfwc_additional_autocomplete_params', 'namespace_cm_tsfwc_additional_autocomplete_params' ); | |
function namespace_cm_tsfwc_additional_autocomplete_params() { | |
return [ | |
'filter_by' => 'catalog_visibility: search || catalog_visibility: visible' | |
]; | |
} | |
// On instant search | |
add_action( 'cm_tsfwc_additional_search_params', 'namespace_cm_tsfwc_additional_search_params' ); | |
function namespace_cm_tsfwc_additional_search_params() { | |
return [ | |
'filter_by' => 'catalog_visibility:visible' | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment