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

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

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