Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/18c9ad74e396cf9eff6a1a9a33997247 to your computer and use it in GitHub Desktop.
Save xlplugins/18c9ad74e396cf9eff6a1a9a33997247 to your computer and use it in GitHub Desktop.
This is a patch snippet used for XL WooCommerce Sales Triggers plugin to hook in YOTPO Reviews to Smarter Reviews Trigger.
<?php
/**
* YOTPO Review System Code
*/
$wcst_plugin_instance = WCST_Core::get_instance();
remove_action('woocommerce_single_product_summary', array($wcst_plugin_instance, 'wcst_position_below_review'), 11);
add_action('woocommerce_single_product_summary', array($wcst_plugin_instance, 'wcst_position_below_review'), 8);
add_filter('wcst_smarter_reviews_single_arr', 'wcst_modify_smarter_reviews_data', 10, 3);
function wcst_modify_smarter_reviews_data($single_product_smarter_reviews_arr, $product_id, $smarter_reviews_single) {
if (empty($product_id)) {
return $single_product_smarter_reviews_arr;
}
$yotpo_settings = get_option('yotpo_settings', true);
if (isset($yotpo_settings['app_key']) && isset($single_product_smarter_reviews_arr['final_display'])) {
$template = $single_product_smarter_reviews_arr['final_display'];
if (isset($smarter_reviews_single['switch_to_max']) && $smarter_reviews_single['switch_to_max'] == 'yes') {
$yotpo_satisfaction_rate_data = wcst_get_review_template('satisfaction_rate', $product_id, $smarter_reviews_single, $yotpo_settings['app_key']);
$percentage_val = 0;
if (isset($yotpo_satisfaction_rate_data['satisfaction_rate'])) {
$percentage_val = $yotpo_satisfaction_rate_data['satisfaction_rate']['value'];
$return_arr = $yotpo_satisfaction_rate_data;
}
$yotpo_rating_greater_than_4_data = wcst_get_review_template('rating_greater_than_4', $product_id, $smarter_reviews_single, $yotpo_settings['app_key']);
if (isset($yotpo_rating_greater_than_4_data['rating_greater_than_4']) && $yotpo_rating_greater_than_4_data['rating_greater_than_4']['value'] > $percentage_val) {
$return_arr = $yotpo_rating_greater_than_4_data;
}
return $return_arr;
} else {
if ($template == 'satisfaction_rate') {
$yotpo_satisfaction_rate_data = wcst_get_review_template('satisfaction_rate', $product_id, $smarter_reviews_single, $yotpo_settings['app_key']);
if (isset($yotpo_satisfaction_rate_data['satisfaction_rate'])) {
return $yotpo_satisfaction_rate_data;
}
} elseif ($template == 'rating_greater_than_4') {
$yotpo_rating_greater_than_4_data = wcst_get_review_template('rating_greater_than_4', $product_id, $smarter_reviews_single, $yotpo_settings['app_key']);
if (isset($yotpo_rating_greater_than_4_data['rating_greater_than_4'])) {
return $yotpo_rating_greater_than_4_data;
}
}
}
}
return false;
}
function wcst_get_review_template($template, $product_id, $smarter_reviews_single, $app_key) {
$ret_array = array();
if ($template == 'satisfaction_rate') {
$url = 'https://api.yotpo.com/products/' . $app_key . '/' . $product_id . '/bottomline';
$trasient_val = get_transient('yotpo_bottomline_' . $product_id);
if ($trasient_val) {
$yotpo_product_bottomline = $trasient_val;
} else {
$yotpo_product_bottomline = wp_remote_get($url, array('timeout' => 60));
set_transient('yotpo_bottomline_' . $product_id, $yotpo_product_bottomline, 3600);
}
if (!is_wp_error($yotpo_product_bottomline)) {
$yotpo_product_bottomline = json_decode(wp_remote_retrieve_body($yotpo_product_bottomline), true);
if (isset($yotpo_product_bottomline['status']['code']) && $yotpo_product_bottomline['status']['code'] == 200) {
$bottomline = $yotpo_product_bottomline['response']['bottomline'];
if (isset($bottomline['average_score'])) {
$average_score = $bottomline['average_score'] ? $bottomline['average_score'] : 0;
$total_reviews = $bottomline['total_reviews'] ? $bottomline['total_reviews'] : 0;
$percentage = $bottomline['average_score'] * 20;
if (strpos($percentage, '.') !== FALSE) {
$decimal_count = strlen(substr(strrchr($percentage, "."), 1));
if ($decimal_count > 1) {
$percentage = round($percentage, 2);
}
}
if ($percentage >= $smarter_reviews_single['dont_show_until']) {
$ret_array['final_display'] = 'satisfaction_rate';
$satisfaction_rate_label = $smarter_reviews_single['satisfaction_rate_label'];
$satisfaction_rate_display = str_replace('{{rating_percentage}}', $percentage . '%', $satisfaction_rate_label);
$satisfaction_rate_display = str_replace('{{number_of_review}}', $total_reviews, $satisfaction_rate_display);
$ret_array['satisfaction_rate']['value'] = $percentage;
$ret_array['satisfaction_rate']['html'] = $satisfaction_rate_display;
}
}
}
} else {
wcst_logging('product id: ' . $product_id, "yotpo_logs.txt", "a");
wcst_logging('url: ' . $url, "yotpo_logs.txt", "a");
wcst_logging($yotpo_product_bottomline->get_error_message(), "yotpo_logs.txt", "a");
}
} elseif ($template == 'rating_greater_than_4') {
$url = 'https://api.yotpo.com/v1/widget/' . $app_key . '/products/' . $product_id . '/reviews.json?per_page=50&sort=date';
$trasient_val = get_transient('yotpo_reviews_' . $product_id);
if ($trasient_val) {
$yotpo_product_reviews = $trasient_val;
} else {
$yotpo_product_reviews = wp_remote_get($url, array('timeout' => 60));
set_transient('yotpo_reviews_' . $product_id, $yotpo_product_reviews, 3600);
}
if (!is_wp_error($yotpo_product_reviews)) {
$yotpo_product_reviews = json_decode(wp_remote_retrieve_body($yotpo_product_reviews), true);
if (isset($yotpo_product_reviews['status']['code']) && $yotpo_product_reviews['status']['code'] == 200) {
$reviews = $yotpo_product_reviews['response']['reviews'];
$bottomline = $yotpo_product_reviews['response']['bottomline'];
if (is_array($reviews) && count($reviews) > 0) {
$user_count_gt_4 = 0;
foreach ($reviews as $single_review) {
if ($single_review['score'] >= 4) {
$user_count_gt_4++;
}
}
if ($user_count_gt_4 > 0) {
$percentage = ($user_count_gt_4 / $bottomline['total_review']) * 100;
if (strpos($percentage, '.') !== FALSE) {
$decimal_count = strlen(substr(strrchr($percentage, "."), 1));
if ($decimal_count > 1) {
$percentage = round($percentage, 2);
}
}
if ($percentage >= $smarter_reviews_single['dont_show_until']) {
$ret_array['final_display'] = 'rating_greater_than_4';
$satisfaction_rate_label = $smarter_reviews_single['rating_greater_than_4_label'];
$satisfaction_rate_display = str_replace('{{positive_feedback_percentage}}', $percentage . '%', $satisfaction_rate_label);
$satisfaction_rate_display = str_replace('{{number_of_review}}', $total_reviews, $satisfaction_rate_display);
$ret_array['rating_greater_than_4']['value'] = $percentage;
$ret_array['rating_greater_than_4']['html'] = $satisfaction_rate_display;
}
}
}
}
} else {
wcst_logging('product id: ' . $product_id, "yotpo_logs.txt", "a");
wcst_logging('url: ' . $url, "yotpo_logs.txt", "a");
wcst_logging($yotpo_product_reviews->get_error_message(), "yotpo_logs.txt", "a");
}
}
return $ret_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment