سلام وقت بخیر. ممنون بابت راهنمایی سوال قبل. خیلی کمک کرد.
من این تکه کد رو نوشتم. میخواستم شما هم بررسی کنید و اگه اشکالی داره یا بهتر میتونه پیاده سازی بشه بهم بگید.
برای اینکه کاربر با رفرش صفحه تشکر دوباره کد رو ایجاد نکنه، با کوئری زدن به دیتابیس و جدول posts وجود اون کد رو بررسی کردم.
سوالی که دارم اینه که من این کد رو توی فایل thankyou دارم اجرا میکنم و اگه ببرمش توی فایل functions تم فرزند دیگه اجرا نمیشه چون متغیر order رو ندارم. راهکارش چیه؟ ممنون.
//Get Coupon Codes from DB and Push the Titles to an Array
global $wpdb;
$coupon_codes_results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'shop_coupon'");
$coupon_code = $order->get_order_number() . '-' . $order->get_billing_phone();
$coupon_codes = [];
foreach ($coupon_codes_results as $coupon_codes_result) {
array_push($coupon_codes, $coupon_codes_result->post_title);
}
//Check if Coupon Code is Exist and Create the Coupon
if (!array_search($coupon_code, $coupon_codes)) {
$after_purchase_coupon_ammount = $order->get_total() > 20000000 ? 2 : 1;
$exp_time = time() + 1296000;
$after_purchase_coupon = new WC_Coupon();
$after_purchase_coupon->set_code($order->get_order_number() . '-' . $order->get_billing_phone());
$after_purchase_coupon->set_discount_type('percent');
$after_purchase_coupon->set_amount($after_purchase_coupon_ammount);
$after_purchase_coupon->set_usage_limit(1);
$after_purchase_coupon->set_date_expires($exp_time);
$after_purchase_coupon->set_description('کد تخفیف بعد از یک خرید');
$after_purchase_coupon->save();
}