geminilabs

[glsr_woo_product_rating]

Jun 15th, 2021 (edited)
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Site Reviews: This is a custom shortcode replacement for the Woo Rating module
  5.  *
  6.  * [glsr_woo_product_rating]
  7.  *
  8.  * Shortcode options:
  9.  *   - display_if_empty: true|false
  10.  */
  11. add_shortcode('glsr_woo_product_rating', function ($atts) {
  12.     if (!function_exists('wc_get_product') // make sure Woocommerce is activated
  13.         || !function_exists('glsr') // make sure Site Reviews is activated
  14.         || !function_exists('glsr_get_ratings')
  15.         || !function_exists('glsr_get_option')
  16.         || !function_exists('glsr_star_rating')) {
  17.         return;
  18.     }
  19.     global $product;
  20.     $wooEnabled = 'yes' === get_option('woocommerce_enable_reviews', 'yes');
  21.     $addonEnabled = glsr_get_option('settings.addons.woocommerce.enabled', false, 'bool');
  22.     if (!$wooEnabled || !$addonEnabled) {
  23.         return;
  24.     }
  25.     $atts = wp_parse_args($atts, [
  26.         'display_if_empty' => true,
  27.     ]);
  28.     $ratings = glsr_get_ratings(['assigned_posts' => 'post_id']);
  29.     if (wp_validate_boolean($atts['display_if_empty']) && $ratings->reviews < 1) {
  30.         return;
  31.     }
  32.     ob_start();
  33.     ?>
  34.     <div class="et_pb_wc_rating et_pb_wc_rating_layout_inline">
  35.         <div class="woocommerce-product-rating <?= glsr('Modules\Style')->styleClasses(); ?>">
  36.             <?= glsr_star_rating($ratings->average); ?>
  37.             <?php if ($product && $product->get_reviews_allowed()) : ?>
  38.                 <a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?= sprintf(_n('%s customer review', '%s customer reviews', $ratings->reviews, 'woocommerce'), '<span class="count">'.esc_html($ratings->reviews).'</span>'); ?>)</a>
  39.             <?php endif; ?>
  40.         </div>
  41.     </div>
  42.     <?php
  43.     return ob_get_clean();
  44. });
Add Comment
Please, Sign In to add comment