PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.2
EmailKit – Email Customizer for WooCommerce & WP v1.6.2
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Emails / Woocommerce / LowStock.php

LowStock.php in EmailKit – Email Customizer for WooCommerce & WP 1.6.2, at includes/Admin/Emails/Woocommerce/LowStock.php

85 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Emails\Woocommerce;
4 use WP_Query;
5 use EmailKit\Admin\Emails\EmailLists;
6 use EmailKit\Admin\Emails\Helpers\Utils;
7
8 defined( 'ABSPATH' ) || exit;
9
10
11
12 class LowStock {
13
14 private $db_query_class = null;
15
16 public function __construct() {
17
18 $args = [
19 'post_type' => 'emailkit',
20 'meta_query' => [
21 [
22 'key' => 'emailkit_template_type',
23 'value' => EmailLists::LOW_STOCK,
24 ],
25 [
26 'key' => 'emailkit_template_status',
27 'value' => 'Active',
28 ],
29 ],
30 ];
31
32 $this->db_query_class = new WP_Query($args);
33
34 if (isset($this->db_query_class->posts[0])) {
35 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
36 }
37
38 add_filter('woocommerce_low_stock_notification', [$this, 'stockNotification'], 10, 1);
39 }
40
41 public function remove_woocommerce_emails($email_class)
42 {
43
44 remove_action('woocommerce_low_stock_notification', array($email_class, 'low_stock'));
45 }
46
47 public function stockNotification($product)
48 {
49
50
51 $query = $this->db_query_class;
52 $email = get_option('admin_email');
53 if (isset($query->posts[0])) {
54 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
55 $tbody = substr($html, strpos($html, '<tbody'));
56 $row = strpos($tbody, '</tbody>');
57 $rows = '';
58 $html = str_replace($row, $rows, $html);
59
60 // Stock details array for email
61 $details = Utils::woocommerce_stock_email_contents($product);
62
63
64 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
65 $to = get_option('admin_email');
66
67 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
68 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
69 $pre_header = !empty($pre_header) ? $pre_header : esc_html__(' Low Stock Amount', 'emailkit');
70 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
71 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
72 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : $product->get_name()." - ". esc_html__(' Low Stock Alert - Take Action Now!', 'emailkit') . ' - ' . $pre_header;
73
74
75
76 $headers = [
77 'From: ' . $email . "\r\n",
78 'Reply-To: ' . $email . "\r\n",
79 'Content-Type: text/html; charset=UTF-8',
80 ];
81
82 wp_mail($to, $subject, $message, $headers);
83 }
84 }
85 }