PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.2.0
EmailKit – Email Customizer for WooCommerce & WP v1.2.0
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.2.0, at includes/Admin/Emails/Woocommerce/NewOrder.php

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