Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Registers the [wc_verified_owner][/wc_verified_owner] shortcode
- *
- * @param array $atts
- * @param string $content
- * @return ?string
- */
- add_shortcode('wc_verified_owner', function ($atts, $content = '') {
- if (!function_exists('wc_customer_bought_product') || !function_exists('wc_get_product')) {
- return;
- }
- if (empty($content)) {
- return;
- }
- $product = wc_get_product(get_the_ID());
- if (!$product) {
- return;
- }
- $defaults = [
- 'message' => esc_html_e('Only logged in customers who have purchased this product may leave a review.', 'woocommerce'),
- ];
- $atts = shortcode_atts($defaults, $atts, 'wc_verified_owner');
- $isProductOwner = wc_customer_bought_product('', get_current_user_id(), $product->get_id());
- $verificationRequired = 'yes' === get_option('woocommerce_review_rating_verification_required');
- if (!$isProductOwner && $verificationRequired) {
- $message = sanitize_text_field($atts['message']);
- if (!empty($message)) {
- $message = sprintf('<p class="woocommerce-verification-required">%s</p>', $message);
- }
- return $message;
- }
- return do_shortcode($content);
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement