Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This code snippet modifies the "Review Reminder" WooCommerce Email.
- *
- * This adds the following validation checks to the Review Reminder email which
- * is sent to a customer after they have made a purchase to remind them to write
- * a review of their purchased products.
- * - 1. Does the customer have the correct user role?
- * - 2. Is this the customer's first order?
- *
- * @param bool $skip Whether the Review Reminder email should be skipped for the provided order
- * @param \WC_Order $order The WooCommerce Order object
- *
- * @return bool - true if the Review Reminder email should be skipped, false otherwise.
- */
- add_filter('site-reviews-notifications/product/reminder/skip', function ($skip, $order) {
- // -------------------------------------
- $role = 'customer'; // change as needed!
- // -------------------------------------
- $user = $order->get_user();
- if (!$user || !in_array($role, $user->roles)) {
- return true; // Skip if user does not have the role
- }
- if (!function_exists('wc_get_customer_order_count') || wc_get_customer_order_count($user->ID) > 1) {
- return true; // Skip if not the first order
- }
- return false; // Proceed with notification
- }, 10, 2);
- /**
- * This code snippet modifies the "Review Notification" emails.
- *
- * This adds the following validation checks to the Review Notification email:
- * - 1. Does the notification have a {wc_coupon} template tag in the message?
- * - 2. Does the logged-in user have the correct user role?
- * - 3. Does the user have only one existing order?
- *
- * @param bool $result Whether the notification conditions have passed
- * @param \GeminiLabs\SiteReviews\Addon\Notifications\Notification $notification The Notification object
- *
- * @return bool - true if the notification should be sent, false otherwise.
- */
- add_filter('site-reviews-notifications/notification/is-valid', function ($result, $notification) {
- // -------------------------------------
- $role = 'customer'; // change as needed!
- // -------------------------------------
- $message = $notification->get('message');
- if (!$result || !str_contains($message, '{wc_coupon}')) {
- // Either the notification conditions have failed
- // Or, the notification does not contain a coupon code template tag
- return $result;
- }
- $user = $notification->review->user();
- if (!$user || !in_array($role, $user->roles)) {
- return false; // Skip if user does not have the role
- }
- if (!function_exists('wc_get_customer_order_count') || wc_get_customer_order_count($user->ID) > 1) {
- return false; // Skip if not the first order
- }
- if (!empty(get_user_meta($user->ID, '_coupon_email_sent', true))) {
- return false; // Skip if user has already been sent a coupon
- }
- update_user_meta($user->ID, '_coupon_email_sent', 1);
- return true; // Proceed with notification
- }, 10, 2);
- /**
- * This code snippet modifies the "Review Notification" emails.
- *
- * This replaces the {wc_coupon} template tag in a Review Notification email to a
- * single-use discount code with a default discount of 10%.
- *
- * To change the discount amount, add the number to the template tag.
- * For example, set a 15% discount like this:
- *
- * {wc_coupon:15}.
- *
- * Change the discount type by adding the type to the template tag after the amount using a pipe character.
- * For example, set a fixed monetary discount of 25 to the entire cart total like this:
- *
- * {wc_coupon:25|fixed_cart}
- *
- * @return string
- */
- add_filter('site-reviews-notifications/email/message', function ($content) {
- if (!function_exists('wc_get_coupon_id_by_code')) {
- return $content; // Abort if WooCommerce is not active
- }
- // This regex pattern matches:
- // - {wc_coupon}: No parameters, uses defaults (10% discount).
- // - {wc_coupon:15}: Specifies a discount amount of 15%.
- // - {wc_coupon:15|percent}: Specifies a discount amount of 15%.
- // - {wc_coupon:25|fixed_cart}: Specifies a 25-unit discount applied to the cart total.
- // - {wc_coupon:25|fixed_product}: Specifies a 25-unit discount applied to each eligible product in the cart.
- $regex = '/{wc_coupon(?::(\d+(?:\.\d+)?)(?:\|(percent|fixed_cart|fixed_product))?)?}/';
- return preg_replace_callback($regex, function ($match) {
- // Extract discount amount if present, otherwise default to 10
- $amount = max(0, (float) ($match[1] ?? 10));
- // Extract discount type if present, otherwise default to "percent"
- $type = $match[2] ?? 'percent';
- do {
- $prefix = 'THANKYOU-'; // prefix the coupon code with this
- $code = $prefix.strtoupper(wp_generate_password(8, false));
- } while (wc_get_coupon_id_by_code($code) !== 0);
- $coupon = new \WC_Coupon();
- $coupon->set_code($code);
- $coupon->set_discount_type($type); // Default type is "percent"
- $coupon->set_amount($amount); // Default amount is 10
- $coupon->set_usage_limit(1); // Can only be used once
- $coupon->set_individual_use(true); // Cannot be used with other coupons
- $coupon->set_exclude_sale_items(true); // Exclude sale items
- if ($coupon->save() > 0) {
- return $code;
- }
- glsr_log()->error("Failed to create coupon with code: {$code}");
- return 'COUPON_ERROR';
- }, $content);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement