| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Emails\Woocommerce; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use EmailKit\Admin\Emails\EmailLists; |
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
class BackOrder { |
| 10 |
|
| 11 |
private $db_query_class = null; |
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
$args = [ |
| 15 |
'post_type' => 'emailkit', |
| 16 |
'meta_query' => [ |
| 17 |
[ |
| 18 |
'key' => 'emailkit_template_type', |
| 19 |
'value' => EmailLists::BACK_ORDER, |
| 20 |
], |
| 21 |
[ |
| 22 |
'key' => 'emailkit_template_status', |
| 23 |
'value' => 'Active', |
| 24 |
], |
| 25 |
], |
| 26 |
]; |
| 27 |
|
| 28 |
|
| 29 |
$this->db_query_class = new WP_Query($args); |
| 30 |
|
| 31 |
if (isset($this->db_query_class->posts[0])) { |
| 32 |
add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']); |
| 33 |
} |
| 34 |
|
| 35 |
add_filter('woocommerce_product_on_backorder_notification', [$this, 'stockNotification'], 10, 1); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
public function remove_woocommerce_emails($email_class) |
| 40 |
{ |
| 41 |
|
| 42 |
remove_action('woocommerce_product_on_backorder_notification', [$email_class, 'backorder']); |
| 43 |
} |
| 44 |
|
| 45 |
public function stockNotification($product) |
| 46 |
{ |
| 47 |
|
| 48 |
|
| 49 |
$query = $this->db_query_class; |
| 50 |
$email = get_option('admin_email'); |
| 51 |
if (isset($query->posts[0])) { |
| 52 |
$html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true); |
| 53 |
$tbody = substr($html, strpos($html, '<tbody')); |
| 54 |
$row = strpos($tbody, '</tbody>'); |
| 55 |
$rows = ''; |
| 56 |
$html = str_replace($row, $rows, $html); |
| 57 |
|
| 58 |
$details = [ |
| 59 |
|
| 60 |
"{{stock_status}}" => $product['product']->get_stock_status(), |
| 61 |
"{{product_name}}" => $product['product']->get_name(), |
| 62 |
"{{stock_quantity}}" => $product['product']->get_stock_quantity(), |
| 63 |
"{{status}}" => $product['product']->get_status(), |
| 64 |
"{{product_id}}" => $product['product']->get_id(), |
| 65 |
"{{short_description}}" => $product['product']->get_short_description(), |
| 66 |
"{{product_price}}" => $product['product']->get_price(), |
| 67 |
"{{manage_stock}}" => $product['product']->get_manage_stock(), |
| 68 |
"{{sku}}" => $product['product']->get_sku(), |
| 69 |
"{{backorders}}" => $product['product']->get_backorders(), |
| 70 |
"{{site_name}}" => get_bloginfo('name'), |
| 71 |
"{{display_name}}" => wp_get_current_user()->display_name, |
| 72 |
|
| 73 |
]; |
| 74 |
$message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html)); |
| 75 |
$to = get_option('admin_email'); |
| 76 |
|
| 77 |
$subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true)); |
| 78 |
$pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true); |
| 79 |
$pre_header = !empty($pre_header) ? $pre_header : esc_html__(' Product is on backorder', 'emailkit'); |
| 80 |
$subject = !empty($subject) ? $subject . ' - ' . $pre_header : $product['product']->get_name() . " - " .esc_html__(' is on backorder', 'emailkit').' - ' . $pre_header; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
$headers = [ |
| 85 |
'From: ' . $email . "\r\n", |
| 86 |
'Reply-To: ' . $email . "\r\n", |
| 87 |
'Content-Type: text/html; charset=UTF-8', |
| 88 |
]; |
| 89 |
|
| 90 |
wp_mail($to, $subject, $message, $headers); |
| 91 |
} |
| 92 |
} |
| 93 |
} |