users} AS users WHERE users.ID NOT IN ( SELECT DISTINCT postmeta.meta_value FROM {$wpdb->postmeta} AS postmeta WHERE postmeta.meta_key = '_customer_user' AND postmeta.meta_value > 0 ) AND DATEDIFF(NOW(), users.user_registered) >= %d "; $eligible_users = $wpdb->get_results($wpdb->prepare($query, $days_since_registration)); if (!empty($eligible_users)) { foreach ($eligible_users as $user) { $user_id = $user->ID; // بررسی که آیا قبلاً برای این کاربر و شرط خاص کد تخفیف ایجاد شده است $existing_code = get_user_meta($user_id, "discount_code_for_{$days_since_registration}_days_{$discount_percentage}", true); if (empty($existing_code)) { // ایجاد کد تخفیف $discount_code = generate_discount_code($user_id, $discount_percentage); // ذخیره کد تخفیف در متادیتای کاربر update_user_meta($user_id, "discount_code_for_{$days_since_registration}_days_{$discount_percentage}", $discount_code); echo ' '.$user_id; } } } } } function generate_discount_code($user_id, $discount_percentage) { $coupon_code = wp_generate_password(10, false); // ایجاد یک کد منحصر به فرد // تنظیمات کوپن $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $new_coupon_id = wp_insert_post($coupon); // تنظیمات مربوط به کوپن update_post_meta($new_coupon_id, 'discount_type', 'percent'); update_post_meta($new_coupon_id, 'coupon_amount', $discount_percentage); update_post_meta($new_coupon_id, 'individual_use', 'yes'); update_post_meta($new_coupon_id, 'product_ids', ''); update_post_meta($new_coupon_id, 'exclude_product_ids', ''); update_post_meta($new_coupon_id, 'usage_limit', '1'); update_post_meta($new_coupon_id, 'expiry_date', ''); update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); update_post_meta($new_coupon_id, 'free_shipping', 'no'); // ارسال پیامک $user_info = get_userdata($user_id); // اطلاعات کاربر if (!empty($user_info)) { $user_nicename = $user_info->user_nicename; // user_nicename date_default_timezone_set("Asia/Tehran"); // your sms.ir panel configuration $APIKey = get_option('smsir_api_key'); $SecretKey = get_option('smsir_secret_key'); $LineNumber = "30004747473604"; $APIURL = "https://ws.sms.ir/"; // your mobile numbers $MobileNumbers = array($user_nicename); $mytext = get_option('sms_text_periodic_discount'); $mytext = str_replace('(d)', '30', $mytext); $mytext = str_replace('(f)', $discount_percentage, $mytext); echo $mytext = str_replace('(e)', $coupon_code, $mytext); // your text messages $Messages = array($mytext); // sending date @$SendDateTime = date("Y-m-d") . "T" . date("H:i:s"); $SmsIR_SendMessage = new SmsIR_SendMessage($APIKey, $SecretKey, $LineNumber, $APIURL); $SendMessage = $SmsIR_SendMessage->sendMessage($MobileNumbers, $Messages, $SendDateTime); if (get_option('default_sms_system') === 'sms_ir') { $SmsIR_SendMessage = new SmsIR_SendMessage($APIKey, $SecretKey, $LineNumber, $APIURL); $SendMessage = $SmsIR_SendMessage->sendMessage($MobileNumbers, $Messages, $SendDateTime); } if (get_option('default_sms_system') === 'kavenegar') { echo file_get_contents('http://api.kavenegar.com/v1/'.get_option('kavenegar_api_key').'/verify/lookup.json?receptor='.$user_nicename.'&token='.$coupon_code.'&&token2='.$discount_percentage.'&token3=30&template=per-order-h'); } } return $coupon_code; } // اجرای تابع find_users_and_create_discounts();