Skip to content

Commit

Permalink
Merge pull request #135 from w3bdesign/develop
Browse files Browse the repository at this point in the history
Make Algolia work with variable products
  • Loading branch information
w3bdesign authored Mar 2, 2021
2 parents 623877e + aed40cb commit 66a44cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
6 changes: 3 additions & 3 deletions algolia-woo-indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Requires at least: 5.5
* Tested up to: 5.5
* Requires PHP: 7.3
* WC requires at least: 4.6.1
* WC tested up to: 4.6.1
* Version: 1.0.45
* WC requires at least: 5.0.0
* WC tested up to: 5.0.0
* Version: 1.0.46
*
* @package algolia-woo-indexer
* @license GNU version 3
Expand Down
69 changes: 41 additions & 28 deletions classes/class-send-products.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Algolia Woo Indexer class for sending products
* Called from main plugin file algolia-woo-indexer.php
Expand All @@ -13,27 +14,27 @@
/**
* Abort if this file is called directly
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
if (!defined('ABSPATH')) {
exit;
}

/**
* Include plugin file if function is_plugin_active does not exist
*/
if (! function_exists('is_plugin_active')) {
if (!function_exists('is_plugin_active')) {
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
}

/**
* Define the plugin version and the database table name
*/
define( 'ALGOWOO_DB_OPTION', '_algolia_woo_indexer' );
define( 'ALGOWOO_CURRENT_DB_VERSION', '0.3' );
define('ALGOWOO_DB_OPTION', '_algolia_woo_indexer');
define('ALGOWOO_CURRENT_DB_VERSION', '0.3');

/**
* Define application constants
*/
define( 'CHANGE_ME', 'change me' );
define('CHANGE_ME', 'change me');

/**
* Database table names
Expand All @@ -43,7 +44,7 @@
define('ALGOLIA_APPLICATION_ID', '_application_id');
define('ALGOLIA_API_KEY', '_admin_api_key');

if (! class_exists('Algolia_Send_Products')) {
if (!class_exists('Algolia_Send_Products')) {
/**
* Algolia WooIndexer main class
*/
Expand Down Expand Up @@ -80,11 +81,11 @@ function () {
}

/**
* Send WooCommerce products to Algolia
*
* @param Int $id Product to send to Algolia if we send only a single product
* @return void
*/
* Send WooCommerce products to Algolia
*
* @param Int $id Product to send to Algolia if we send only a single product
* @return void
*/
public static function send_products_to_algolia($id = '')
{
/**
Expand All @@ -102,10 +103,10 @@ public static function send_products_to_algolia($id = '')
$algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME;

$algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
$algolia_api_key = is_string($algolia_api_key) ?$algolia_api_key : CHANGE_ME;
$algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME;

$algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
$algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;
$algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME;

/**
* Display admin notice and return if not all values have been set
Expand All @@ -122,7 +123,7 @@ public static function send_products_to_algolia($id = '')
* Check if we can connect, if not, handle the exception, display an error and then return
*/
self::can_connect_to_algolia();

/**
* Initialize the search index and set the name to the option from the database
*/
Expand All @@ -140,12 +141,12 @@ public static function send_products_to_algolia($id = '')
);

/**
* Setup arguments for sending only a single product
*/
* Setup arguments for sending only a single product
*/
if (isset($id) && '' !== $id) {
$arguments = array(
'status' => 'publish',
'include' => array( $id ),
'include' => array($id),
'paginate' => false,
);
}
Expand All @@ -155,7 +156,9 @@ public static function send_products_to_algolia($id = '')
*
* @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html
*/
$products = /** @scrutinizer ignore-call */ wc_get_products($arguments);
$products =
/** @scrutinizer ignore-call */
wc_get_products($arguments);

if (empty($products)) {
return;
Expand All @@ -164,6 +167,17 @@ public static function send_products_to_algolia($id = '')
$record = array();

foreach ($products as $product) {
/**
* Set sale price or regular price based on product type
*/
if ($product->is_type('simple')) {
$sale_price = $product->get_sale_price();
$regular_price = $product->get_regular_price();
} elseif ($product->is_type('variable')) {
$sale_price = $product->get_variation_sale_price('min', true);
$regular_price = $product->get_variation_regular_price('max', true);
}

/**
* Extract image from $product->get_image()
*/
Expand All @@ -172,14 +186,13 @@ public static function send_products_to_algolia($id = '')
/**
* Build the record array using the information from the WooCommerce product
*/
$record['objectID'] = $product->get_id();
$record['product_name'] = $product->get_name();
$record['product_image'] = $product_image;
$record['short_description'] = $product->get_short_description();
$record['regular_price'] = $product->get_regular_price();
$record['sale_price'] = $product->get_sale_price();
$record['on_sale'] = $product->is_on_sale();

$record['objectID'] = $product->get_id();
$record['product_name'] = $product->get_name();
$record['product_image'] = $product_image;
$record['short_description'] = $product->get_short_description();
$record['regular_price'] = $regular_price;
$record['sale_price'] = $sale_price;
$record['on_sale'] = $product->is_on_sale();
$records[] = $record;
}
wp_reset_postdata();
Expand All @@ -202,4 +215,4 @@ public static function send_products_to_algolia($id = '')
</div>';
}
}
}
}

0 comments on commit 66a44cd

Please sign in to comment.