Created
September 23, 2016 11:14
-
-
Save rayrutjes/3cf141719d6b77a977d7650b27ffcc66 to your computer and use it in GitHub Desktop.
This is a boilerplate for using Algolia in WooCommerce
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 | |
/** | |
* @wordpress-plugin | |
* Plugin Name: Search by Algolia for WooCommerce - Instant & Relevant results | |
*/ | |
/** | |
* If Algolia is not active, let users know. | |
**/ | |
if ( ! in_array( 'algolia/algolia.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
add_action( 'admin_notices', function() { | |
echo '<div class="error notice"> | |
<p>' . esc_html__( 'Algolia Search for WooCommerce: Algolia Search plugin should be enabled.', 'algolia-woocommerce' ) . '</p> | |
</div>'; | |
} ); | |
} | |
/** | |
* If WooCommerce is not active, let users know. | |
**/ | |
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
add_action( 'admin_notices', function() { | |
echo '<div class="error notice"> | |
<p>' . esc_html__( 'Algolia Search for WooCommerce: WooCommerce plugin should be enabled.', 'algolia-woocommerce' ) . '</p> | |
</div>'; | |
} ); | |
} | |
/** | |
* @param array $attributes | |
* @param WP_Post $post | |
* | |
* @return array | |
*/ | |
function aw_product_shared_attributes( array $attributes, WP_Post $post ) { | |
$product = wc_get_product( $post ); | |
$attributes['product_type'] = (string) $product->get_type(); | |
$attributes['display_price'] = (float) $product->get_display_price(); | |
$attributes['price'] = (float) $product->get_price(); | |
$attributes['price_excluding_tax'] = (float) $product->get_price_excluding_tax(); | |
$attributes['regular_price'] = (float) $product->get_regular_price(); | |
$attributes['sale_price'] = (float) $product->get_sale_price(); | |
$attributes['sale_price'] = (float) $product->get_price(); | |
$attributes['average_rating'] = (float) $product->get_average_rating(); | |
$attributes['rating_count'] = (int) $product->get_rating_count(); | |
$attributes['attributes'] = (array) $product->get_attributes(); | |
$attributes['width'] = (string) $product->get_width(); | |
$attributes['height'] = (string) $product->get_height(); | |
$attributes['weight'] = (string) $product->get_weight(); | |
$attributes['length'] = (string) $product->get_length(); | |
$attributes['review_count'] = (int) $product->get_review_count(); | |
$attributes['dimensions'] = (string) $product->get_dimensions(); | |
$attributes['total_sales'] = (int) get_post_meta( $post->ID, 'total_sales', true ); | |
return $attributes; | |
} | |
add_filter( 'algolia_post_product_shared_attributes', 'aw_product_shared_attributes', 10, 2 ); | |
add_filter( 'algolia_searchable_post_product_shared_attributes', 'aw_product_shared_attributes', 10, 2 ); | |
/** | |
* @param bool $should_index | |
* @param WP_Post $post | |
* | |
* @return bool | |
*/ | |
function aw_should_index_post( $should_index, WP_Post $post ) { | |
// Only alter decision making if we are dealing with a product. | |
if ( 'product' !== $post->post_type ) { | |
return $should_index; | |
} | |
// This is required as `is_visible` method also checks for user_cap. | |
if( 'publish' !== $post->post_status ) { | |
return false; | |
} | |
$product = wc_get_product( $post ); | |
// We extracted this check because `is_visible` will not detect searchable products if not in a loop. | |
if ( 'search' === $product->visibility ) { | |
return true; | |
} | |
return $product->is_visible(); | |
} | |
add_filter( 'algolia_should_index_post', 'aw_should_index_post', 10, 2 ); | |
add_filter( 'algolia_should_index_searchable_post', 'aw_should_index_post', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment