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 / NewOrder.php

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

167 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EmailKit\Admin\Emails\Woocommerce;
3
4 use WP_Query;
5 use EmailKit\Admin\Emails\EmailLists;
6
7 defined('ABSPATH') || exit;
8
9 class NewOrder
10 {
11
12 private $db_query_class = null;
13
14 public function __construct()
15 {
16
17 $args = array(
18 'post_type' => 'emailkit',
19 'meta_query' => array(
20 array(
21 'key' => 'emailkit_template_type',
22 'value' => EmailLists::NEW_ORDER,
23 ),
24 array(
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_order_status_pending_to_processing_notification', [$this, 'newOrderEmail'], 10, 2);
38 add_filter('woocommerce_order_status_pending_to_on-hold_notification', [$this, 'newOrderEmail'], 10, 2);
39
40 }
41
42 /**
43 * remove action to disable default mail
44 */
45
46 public function remove_woocommerce_emails($email_class)
47 {
48
49 remove_action('woocommerce_order_status_pending_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
50 remove_action('woocommerce_order_status_pending_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
51 remove_action('woocommerce_order_status_pending_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
52 remove_action('woocommerce_order_status_failed_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
53 remove_action('woocommerce_order_status_failed_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
54 remove_action('woocommerce_order_status_failed_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
55 remove_action('woocommerce_order_status_cancelled_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
56 remove_action('woocommerce_order_status_cancelled_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
57 remove_action('woocommerce_order_status_cancelled_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
58 }
59
60
61 public function newOrderEmail($order_id, $order)
62 {
63
64 $query = $this->db_query_class;
65 $email = get_option('admin_email');
66
67 if (isset($query->posts[0])) {
68
69 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
70
71 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
72 $replacements = [];
73 foreach ($order->get_items() as $item_id => $item) {
74 $product = $item->get_product();
75 $id = $item['product_id'];
76 $product_name = $item['name'];
77 $item_qty = $item['quantity'];
78 $item_total = $item['total'];
79 $product_price = $product->get_price() * $item_qty;
80
81 // Use the currency symbol from the WooCommerce settings
82 $currency_symbol = $woocommerce_currency_settings;
83
84 // Format the product price with the currency symbol
85 $formatted_product_price = $currency_symbol . number_format($product_price, 2);
86
87 // Format the item total with the currency symbol
88 $formatted_item_total = $currency_symbol . number_format($item_total, 2);
89
90
91 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
92
93 }
94
95 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
96
97
98 $order = wc_get_order($order_id);
99
100 $shipping_first_name = $order->get_shipping_first_name();
101 $shipping_last_name = $order->get_shipping_last_name();
102 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
103
104 $details = [
105 "{{order_id}}" => $order->get_id(),
106 "{{order_number}}" => $order->get_order_number(),
107 "{{order_status}}" => $order->get_status(),
108 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
109 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
110 "{{order_currency}}" => $order->get_currency(),
111 "{{billing_phone}}" => $order->get_billing_phone(),
112 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
113 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
114 "{{shipping_method}}" => $order->get_shipping_method(),
115 "{{payment_method}}" => $order->get_payment_method_title(),
116 "{{total}}" => wc_price($order->get_total(), 2),
117 "{{billing_name}}" => $order->get_formatted_billing_full_name(),
118 "{{billing_first_name}}" => $order->get_billing_first_name(),
119 "{{billing_last_name}}" => $order->get_billing_last_name(),
120 "{{billing_company}}" => $order->get_billing_company(),
121 "{{billing_address_1}}" => $order->get_billing_address_1(),
122 "{{billing_address_2}}" => $order->get_billing_address_2(),
123 "{{billing_city}}" => $order->get_billing_city(),
124 "{{billing_state}}" => $order->get_billing_state(),
125 "{{billing_postcode}}" => $order->get_billing_postcode(),
126 "{{billing_country}}" => $order->get_billing_country(),
127 "{{billing_email}}" => $order->get_billing_email(),
128 "{{shipping_name}}" => $shipping_full_name,
129 "{{shipping_first_name}}" => $shipping_first_name,
130 "{{shipping_last_name}}" => $shipping_last_name,
131 "{{shipping_company}}" => $order->get_shipping_company(),
132 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
133 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
134 "{{shipping_city}}" => $order->get_shipping_city(),
135 "{{shipping_state}}" => $order->get_shipping_state(),
136 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
137 "{{shipping_country}}" => $order->get_shipping_country(),
138 "{{shipping_phone}}" => $order->get_shipping_phone(),
139 "{{customer_note}}" => $order->get_customer_note(),
140 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
141 ];
142
143
144
145
146
147 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
148 $to = get_option('admin_email');
149
150 $pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
151 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("order placed on your store", "emailkit");
152 $subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true));
153 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", ". esc_html__("placed a new order#" , "emailkit") . esc_attr($order_id) . esc_html__(" on your store", "emailkit"). ' - ' . $pre_header;
154
155
156
157 $headers = [
158 'From: ' . $email . "\r\n",
159 'Reply-To: ' . $email . "\r\n",
160 'Content-Type: text/html; charset=UTF-8',
161 ];
162
163 wp_mail($to, $subject, $message, $headers);
164 }
165 }
166 }
167