Advertisement
geminilabs

Untitled

Mar 16th, 2025 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.39 KB | None | 0 0
  1. /**
  2.  * This code snippet modifies the "Review Reminder" WooCommerce Email.
  3.  *
  4.  * This adds the following validation checks to the Review Reminder email which
  5.  * is sent to a customer after they have made a purchase to remind them to write
  6.  * a review of their purchased products.
  7.  * - 1. Does the customer have the correct user role?
  8.  * - 2. Is this the customer's first order?
  9.  *
  10.  * @param bool $skip  Whether the Review Reminder email should be skipped for the provided order
  11.  * @param \WC_Order $order  The WooCommerce Order object
  12.  *
  13.  * @return bool - true if the Review Reminder email should be skipped, false otherwise.
  14.  */
  15. add_filter('site-reviews-notifications/product/reminder/skip', function ($skip, $order) {
  16.     // -------------------------------------
  17.     $role = 'customer'; // change as needed!
  18.     // -------------------------------------
  19.     $user = $order->get_user();
  20.     if (!$user || !in_array($role, $user->roles)) {
  21.         return true; // Skip if user does not have the role
  22.     }
  23.     if (!function_exists('wc_get_customer_order_count') || wc_get_customer_order_count($user->ID) > 1) {
  24.         return true; // Skip if not the first order
  25.     }
  26.     return false; // Proceed with notification
  27. }, 10, 2);
  28.  
  29. /**
  30.  * This code snippet modifies the "Review Notification" emails.
  31.  *
  32.  * This adds the following validation checks to the Review Notification email:
  33.  * - 1. Does the notification have a {wc_coupon} template tag in the message?
  34.  * - 2. Does the logged-in user have the correct user role?
  35.  * - 3. Does the user have only one existing order?
  36.  *
  37.  * @param bool $result  Whether the notification conditions have passed
  38.  * @param \GeminiLabs\SiteReviews\Addon\Notifications\Notification $notification  The Notification object
  39.  *
  40.  * @return bool - true if the notification should be sent, false otherwise.
  41.  */
  42. add_filter('site-reviews-notifications/notification/is-valid', function ($result, $notification) {
  43.     // -------------------------------------
  44.     $role = 'customer'; // change as needed!
  45.     // -------------------------------------
  46.     $message = $notification->get('message');
  47.     if (!$result || !str_contains($message, '{wc_coupon}')) {
  48.         // Either the notification conditions have failed
  49.         // Or, the notification does not contain a coupon code template tag
  50.         return $result;
  51.     }
  52.     $user = $notification->review->user();
  53.     if (!$user || !in_array($role, $user->roles)) {
  54.         return false; // Skip if user does not have the role
  55.     }
  56.     if (!function_exists('wc_get_customer_order_count') || wc_get_customer_order_count($user->ID) > 1) {
  57.         return false; // Skip if not the first order
  58.     }
  59.     if (!empty(get_user_meta($user->ID, '_coupon_email_sent', true))) {
  60.         return false; // Skip if user has already been sent a coupon
  61.     }
  62.     update_user_meta($user->ID, '_coupon_email_sent', 1);
  63.     return true; // Proceed with notification
  64. }, 10, 2);
  65.  
  66. /**
  67.  * This code snippet modifies the "Review Notification" emails.
  68.  *
  69.  * This replaces the {wc_coupon} template tag in a Review Notification email to a
  70.  * single-use discount code with a default discount of 10%.
  71.  *
  72.  * To change the discount amount, add the number to the template tag.
  73.  * For example, set a 15% discount like this:
  74.  *
  75.  * {wc_coupon:15}.
  76.  *
  77.  * Change the discount type by adding the type to the template tag after the amount using a pipe character.
  78.  * For example, set a fixed monetary discount of 25 to the entire cart total like this:
  79.  *
  80.  * {wc_coupon:25|fixed_cart}
  81.  *
  82.  * @return string
  83.  */
  84. add_filter('site-reviews-notifications/email/message', function ($content) {
  85.     if (!function_exists('wc_get_coupon_id_by_code')) {
  86.         return $content; // Abort if WooCommerce is not active
  87.     }
  88.     // This regex pattern matches:
  89.     // - {wc_coupon}: No parameters, uses defaults (10% discount).
  90.     // - {wc_coupon:15}: Specifies a discount amount of 15%.
  91.     // - {wc_coupon:15|percent}: Specifies a discount amount of 15%.
  92.     // - {wc_coupon:25|fixed_cart}: Specifies a 25-unit discount applied to the cart total.
  93.     // - {wc_coupon:25|fixed_product}: Specifies a 25-unit discount applied to each eligible product in the cart.
  94.     $regex = '/{wc_coupon(?::(\d+(?:\.\d+)?)(?:\|(percent|fixed_cart|fixed_product))?)?}/';
  95.     return preg_replace_callback($regex, function ($match) {
  96.         // Extract discount amount if present, otherwise default to 10
  97.         $amount = max(0, (float) ($match[1] ?? 10));
  98.         // Extract discount type if present, otherwise default to "percent"
  99.         $type = $match[2] ?? 'percent';
  100.         do {
  101.             $prefix = 'THANKYOU-'; // prefix the coupon code with this
  102.             $code = $prefix.strtoupper(wp_generate_password(8, false));
  103.         } while (wc_get_coupon_id_by_code($code) !== 0);
  104.         $coupon = new \WC_Coupon();
  105.         $coupon->set_code($code);
  106.         $coupon->set_discount_type($type); // Default type is "percent"
  107.         $coupon->set_amount($amount); // Default amount is 10
  108.         $coupon->set_usage_limit(1); // Can only be used once
  109.         $coupon->set_individual_use(true); // Cannot be used with other coupons
  110.         $coupon->set_exclude_sale_items(true); // Exclude sale items
  111.         if ($coupon->save() > 0) {
  112.             return $code;
  113.         }
  114.         glsr_log()->error("Failed to create coupon with code: {$code}");
  115.         return 'COUPON_ERROR';
  116.     }, $content);
  117. });
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement