Advertisement
geminilabs

[site-reviews] [wc_verified_owner] shortcode

Sep 28th, 2023 (edited)
2,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Registers the [wc_verified_owner][/wc_verified_owner] shortcode
  3.  *
  4.  * @param array $atts
  5.  * @param string $content
  6.  * @return ?string
  7.  */
  8. add_shortcode('wc_verified_owner', function ($atts, $content = '') {
  9.     if (!function_exists('wc_customer_bought_product') || !function_exists('wc_get_product')) {
  10.         return;
  11.     }
  12.     if (empty($content)) {
  13.         return;
  14.     }
  15.     $product = wc_get_product(get_the_ID());
  16.     if (!$product) {
  17.         return;
  18.     }
  19.     $defaults = [
  20.         'message' => esc_html_e('Only logged in customers who have purchased this product may leave a review.', 'woocommerce'),
  21.     ];
  22.     $atts = shortcode_atts($defaults, $atts, 'wc_verified_owner');
  23.     $isProductOwner = wc_customer_bought_product('', get_current_user_id(), $product->get_id());
  24.     $verificationRequired = 'yes' === get_option('woocommerce_review_rating_verification_required');
  25.     if (!$isProductOwner && $verificationRequired) {
  26.         $message = sanitize_text_field($atts['message']);
  27.         if (!empty($message)) {
  28.             $message = sprintf('<p class="woocommerce-verification-required">%s</p>', $message);
  29.         }
  30.         return $message;
  31.     }
  32.     return do_shortcode($content);
  33. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement