PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.2
EmailKit – Email Customizer for WooCommerce & WP v1.5.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.5.2, at includes/Admin/Emails/Woocommerce/LowStock.php

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