Created
November 26, 2018 19:55
-
-
Save jchristopher/c73fd3a2977113e26f33f75eb9a60f87 to your computer and use it in GitHub Desktop.
SearchWP BigCommerce integration
This file contains 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_filter( 'searchwp_extra_metadata', function( $extra_meta, $post_being_indexed ) { | |
// BigCommerce Custom Fields are stored in a single record | |
$big_commerce_meta = get_post_meta( $post_being_indexed->ID, 'bigcommerce_custom_fields', true ); | |
if ( empty( $big_commerce_meta ) ) { | |
return $extra_meta; | |
} | |
// Index BigCommerce Custom Fields as individual extra metadata in the SearchWP index | |
foreach ( $big_commerce_meta as $bigcommerce_custom_field ) { | |
$extra_meta[ $bigcommerce_custom_field['name'] ] = $bigcommerce_custom_field['value']; | |
} | |
return $extra_meta; | |
}, 10, 2 ); | |
add_filter( 'searchwp_custom_field_keys', function( $keys ) { | |
global $wpdb; | |
$big_commerce_meta_values = $wpdb->get_results(" | |
SELECT $wpdb->postmeta.meta_value | |
FROM $wpdb->postmeta | |
WHERE $wpdb->postmeta.meta_key = 'bigcommerce_custom_fields'", | |
ARRAY_A ); | |
if ( empty( $big_commerce_meta_values ) ) { | |
return $keys; | |
} | |
$bigcommerce_keys = array(); | |
foreach ( $big_commerce_meta_values as $big_commerce_meta_value ) { | |
$custom_field_record = maybe_unserialize( $big_commerce_meta_value['meta_value'] ); | |
if ( empty( $custom_field_record ) ) { | |
continue; | |
} | |
$bigcommerce_keys = array_merge( $bigcommerce_keys, wp_list_pluck( $custom_field_record, 'name' ) ); | |
} | |
$bigcommerce_keys = array_unique( $bigcommerce_keys ); | |
return array_merge( $keys, $bigcommerce_keys ); | |
}, 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment