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

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

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