get_results( "SELECT * FROM `notifier_table` WHERE `offer_id`=$offer_id AND `confirmed`='Y'" ); foreach ( $items as $item ) { $permalink = get_permalink( $offer_id ); $email = $item->email; add_filter( 'wp_mail_content_type', function($content_type){ return "text/html"; }); $product_id = $item->product_id; $product = get_post( $product_id ); $product_name = $product->post_title; $theme = get_option('stock_noti_back_email_theme'); $text = get_option('stock_noti_back_email_html'); $text = str_replace( '{product_link}', $permalink, $text ); $text = str_replace( '{product_name}', $product_name, $text ); $theme = str_replace( '{product_name}', $product_name, $theme ); //wp_mail( $email, $theme, $text); $wpdb->delete( 'notifier_table', array( 'id' => $item->id ) ); //mail( $email, 'You favorite product got back to stock!', $permalink ); global $woocommerce; $mailer = WC()->mailer(); $content = get_custom_email_html(0, $theme, $mailer, $text); $headers = "Content-Type: text/html\r\n"; $mailer->send( $email, $theme, $content, $headers ); } } function AddToWaitList( $product_id, $offer_id, $email ) { global $wpdb; // Post type $post_data = array( 'post_title' => sanitize_text_field( $email ), 'post_type' => 'product_subscription', 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, ); $post_id = wp_insert_post( $post_data ); update_post_meta( $post_id, 'product_id', $product_id ); update_post_meta( $post_id, 'offer_id', $offer_id ); update_post_meta( $post_id, 'confirmed', '' ); $product = get_post( $product_id ); $product_name = $product->post_title; $items = $wpdb->get_results( "SELECT * FROM `notifier_table` WHERE `offer_id`=$offer_id AND `product_id`=$product_id AND `email`='$email'" ); if ($items) { foreach ( $items as $item ) { $wpdb->delete( 'notifier_table', array( 'id' => $item->id ) ); } } // DB $noti_hash = uniqid(); $query = "INSERT INTO `notifier_table` (`product_id`, `offer_id`, `email`, `post_id`, `noti_hash`) VALUES ($product_id, $offer_id, '$email', '$post_id', '$noti_hash');"; $wpdb->query( $query ); $notif_if = $wpdb->insert_id; // Email add_filter( 'wp_mail_content_type', function($content_type){ return "text/html"; }); $permalink = get_permalink( $offer_id ); // Confirm link $confirm_link = $_SERVER['SERVER_NAME'].'/?confirm_noti='.$noti_hash; $theme = get_option('stock_noti_confirmation_email_theme'); $text = get_option('stock_noti_confirmation_email_html'); $text = str_replace( '{product_link}', $permalink, $text ); $text = str_replace( '{product_confirm_subscription}', $confirm_link, $text ); $text = str_replace( '{product_name}', $product_name, $text ); $theme = str_replace( '{product_name}', $product_name, $theme ); //wp_mail( $email, $theme, $text); global $woocommerce; $mailer = WC()->mailer(); $content = get_custom_email_html(0, $theme, $mailer, $text); $headers = "Content-Type: text/html\r\n"; $mailer->send( $email, $theme, $content, $headers ); } function ConfirmNoti( $noti_hash ) { global $wpdb; $items = $wpdb->get_results( "SELECT * FROM `notifier_table` WHERE `noti_hash`='$noti_hash'" ); $item = $items[0]; $post_id = $item->post_id; update_post_meta( $post_id, 'confirmed', 'Y' ); $wpdb->update( 'notifier_table', [ 'confirmed' => 'Y' ], [ 'noti_hash' => $noti_hash ] ); // Email $offer_id = $item->offer_id; $permalink = get_permalink( $offer_id ); $email = $item->email; $product_id = $item->product_id; $product = get_post( $product_id ); $product_name = $product->post_title; add_filter( 'wp_mail_content_type', function($content_type){ return "text/html"; }); // Unscribe link $unscribe_link = $_SERVER['SERVER_NAME'].'/?cancel_noti='.$noti_hash; $theme = get_option('stock_noti_confirmed_email_theme'); $text = get_option('stock_noti_confirmed_email_html'); $text = str_replace( '{product_link}', $permalink, $text ); $text = str_replace( '{product_cancel_subscription}', $unscribe_link, $text ); $text = str_replace( '{product_name}', $product_name, $text ); $theme = str_replace( '{product_name}', $product_name, $theme ); //wp_mail( $email, $theme, $text); //$wpdb->delete( 'notifier_table', array( 'id' => $item->id ) ); global $woocommerce; $mailer = WC()->mailer(); $content = get_custom_email_html(0, $theme, $mailer, $text); $headers = "Content-Type: text/html\r\n"; $mailer->send( $email, $theme, $content, $headers ); } function CancelNoti( $noti_hash ) { global $wpdb; $items = $wpdb->get_results( "SELECT * FROM `notifier_table` WHERE `noti_hash`='$noti_hash'" ); $item = $items[0]; $post_id = $item->post_id; update_post_meta( $post_id, 'confirmed', '' ); $wpdb->update( 'notifier_table', [ 'confirmed' => '' ], [ 'noti_hash' => $noti_hash ] ); } function CheckAEmailExists( $offer_id, $email ) { global $wpdb; $sql = "SELECT * FROM `notifier_table` WHERE offer_id='$offer_id' AND email='$email'"; $items = $wpdb->get_results( $sql ); return count( $items ); } function get_custom_email_html($order, $subject, $mailer, $text) { $template = 'emails/my-custom-email-i-want-to-send.php'; return wc_get_template_html( $template, array( 'order' => $order, //'email_heading' => $heading, 'subject' => $subject, 'sent_to_admin' => false, 'plain_text' => false, 'custom_text' => $text, 'email' => $mailer ) ); }