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

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

154 lines 5.5 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 use EmailKit\Admin\Emails\Helpers\Utils;
8
9 defined("ABSPATH") || exit;
10
11 class FailedOrder
12 {
13
14 private $db_query_class = null;
15
16 public function __construct()
17 {
18
19 $args = array(
20 'post_type' => 'emailkit',
21 'meta_query' => array(
22 array(
23 'key' => 'emailkit_template_type',
24 'value' => EmailLists::FAILED_ORDER,
25 ),
26 array(
27 'key' => 'emailkit_template_status',
28 'value' => 'Active',
29 ),
30 ),
31 );
32
33
34
35 $this->db_query_class = new WP_Query($args);
36
37 if (isset($this->db_query_class->posts[0])) {
38 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
39 }
40
41 add_filter('woocommerce_order_status_pending_to_failed_notification', [$this, 'failedOrder'], 10, 2);
42 add_filter('woocommerce_order_status_on-hold_to_failed_notification', [$this, 'failedOrder'], 10, 2);
43 }
44
45 public function remove_woocommerce_emails($email_class)
46 {
47
48 remove_action('woocommerce_order_status_pending_to_failed_notification', array($email_class->emails['WC_Email_Failed_Order'], 'trigger'));
49 remove_action('woocommerce_order_status_on-hold_to_failed_notification', array($email_class->emails['WC_Email_Failed_Order'], 'trigger'));
50 }
51
52 public function failedOrder($order_id, $order)
53 {
54
55
56 $query = $this->db_query_class;
57 $email = get_option('admin_email');
58 if (isset($query->posts[0])) {
59 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
60
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
71 // Format the product price with the currency symbol
72 $formatted_product_price = wc_price($product_price);
73
74 // Format the item total with the currency symbol
75 $formatted_item_total = wc_price($item_total);
76
77 $product_image_id = $product->get_image_id();
78 $product_image_url = $product_image_id ? wp_get_attachment_url($product_image_id) : wc_placeholder_img_src();
79
80 $product_sku = $product->get_sku();
81 $attributes = [];
82 $meta_data = $item->get_meta_data();
83
84 foreach ($meta_data as $meta) {
85 // Check for product attribute (pa_ is the prefix for standard attributes)
86 if (strpos($meta->key, 'pa_') === 0) {
87 // Standard product attribute
88 $formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product));
89 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value
90
91 // Add the attribute to the list with HTML markup
92 $attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value);
93
94 } else {
95
96 $formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : '';
97 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : '';
98
99 // Add the custom meta to the list with HTML markup
100 $attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value);
101
102 }
103 }
104
105 $formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : '';
106
107
108 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes];
109
110 }
111
112 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
113
114 $order = wc_get_order($order_id);
115
116 // Order details array for email
117 $details = $details = Utils::woocommerce_order_email_contents($order);
118
119 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
120 $currency_symbol_position = get_option('woocommerce_currency_pos');
121
122 if($currency_symbol_position == 'left') {
123
124 $message = Utils::adjust_price_structure($message);
125
126 } else if($currency_symbol_position == 'left_space') {
127
128 $message = Utils::adjust_left_space_price_structure($message);
129
130 }
131 $email_settings = get_option('woocommerce_failed_order_settings', []);
132 $to = !empty($email_settings['recipient']) ? explode(',', $email_settings['recipient']) : [get_option('admin_email')];
133 $to = array_map('trim', $to);
134
135 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
136 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
137 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("Failed Order ", "emailkit");
138 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
139 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
140 $blog_name = get_option('blogname');
141 $blog_name = '[' . trim($blog_name, '[]') . ']';
142 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : $blog_name .':'. 'Order#'. $order_id. esc_html__(" has been failed.", "emailkit"). ' - ' . $pre_header;
143
144
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 }