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

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

171 lines 7.0 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 use EmailKit\Admin\Emails\Helpers\Utils;
7
8 defined('ABSPATH') || exit;
9
10 class NewOrder
11 {
12
13 private $db_query_class = null;
14
15 public function __construct()
16 {
17
18 $args = array(
19 'post_type' => 'emailkit',
20 'meta_query' => array(
21 array(
22 'key' => 'emailkit_template_type',
23 'value' => EmailLists::NEW_ORDER,
24 ),
25 array(
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_order_status_pending_to_processing_notification', [$this, 'newOrderEmail'], 10, 2);
39 add_filter('woocommerce_order_status_pending_to_on-hold_notification', [$this, 'newOrderEmail'], 10, 2);
40
41 }
42
43 /**
44 * remove action to disable default mail
45 */
46
47 public function remove_woocommerce_emails($email_class)
48 {
49
50 remove_action('woocommerce_order_status_pending_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
51 remove_action('woocommerce_order_status_pending_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
52 remove_action('woocommerce_order_status_pending_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
53 remove_action('woocommerce_order_status_failed_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
54 remove_action('woocommerce_order_status_failed_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
55 remove_action('woocommerce_order_status_failed_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
56 remove_action('woocommerce_order_status_cancelled_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
57 remove_action('woocommerce_order_status_cancelled_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
58 remove_action('woocommerce_order_status_cancelled_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
59 }
60
61
62 public function newOrderEmail($order_id, $order)
63 {
64
65 $query = $this->db_query_class;
66 $email = get_option('admin_email');
67
68 if (isset($query->posts[0])) {
69
70 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
71
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
82 // Format the product price with the currency symbol
83 $formatted_product_price = wc_price($product_price);
84
85 // Format the item total with the currency symbol
86 $formatted_item_total = wc_price($item_total);
87
88 $product_image_id = $product->get_image_id();
89 $product_image_url = $product_image_id ? wp_get_attachment_url($product_image_id) : wc_placeholder_img_src();
90
91 $product_sku = $product->get_sku();
92
93 $attributes = [];
94 $meta_data = $item->get_meta_data();
95
96 foreach ($meta_data as $meta) {
97 // Check for product attribute (pa_ is the prefix for standard attributes)
98 if (strpos($meta->key, 'pa_') === 0) {
99 // Standard product attribute
100 $formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product));
101 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value
102
103 // Add the attribute to the list with HTML markup
104 $attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value);
105
106 } else {
107
108 $formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : '';
109 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : '';
110
111 // Add the custom meta to the list with HTML markup
112 $attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value);
113
114 }
115 }
116
117 $formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : '';
118
119
120 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes];
121
122
123 }
124
125 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
126
127
128 $order = wc_get_order($order_id);
129
130 // Order details array for email
131 $details = Utils::woocommerce_order_email_contents($order);
132
133
134
135 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
136
137 $currency_symbol_position = get_option('woocommerce_currency_pos');
138
139 if($currency_symbol_position == 'left') {
140
141 $message = Utils::adjust_price_structure($message);
142
143 } else if($currency_symbol_position == 'left_space') {
144
145 $message = Utils::adjust_left_space_price_structure($message);
146
147 }
148 $email_settings = get_option('woocommerce_new_order_settings', []);
149 $to = !empty($email_settings['recipient']) ? explode(',', $email_settings['recipient']) : [get_option('admin_email')];
150 $to = array_map('trim', $to);
151
152 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
153 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
154 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("order placed on your store", "emailkit");
155 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
156 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
157 $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;
158
159
160
161 $headers = [
162 'From: ' . $email . "\r\n",
163 'Reply-To: ' . $email . "\r\n",
164 'Content-Type: text/html; charset=UTF-8',
165 ];
166
167 wp_mail($to, $subject, $message, $headers);
168 }
169 }
170 }
171