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

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

177 lines 6.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 EmailKit\Admin\Emails\EmailLists;
6 use WP_Query;
7 use EmailKit\Admin\Emails\Helpers\Utils;
8
9 defined('ABSPATH') || exit;
10
11 class PartialRefund
12 {
13
14 /**
15 * @var mixed
16 */
17 private $db_query_class = null;
18
19 public function __construct()
20 {
21
22
23 $args = [
24 'post_type' => 'emailkit',
25 'meta_query' => [
26 [
27 'key' => 'emailkit_template_type',
28 'value' => EmailLists::PARTIAL_REFUND
29 ],
30 [
31 'key' => 'emailkit_template_status',
32 'value' => 'Active'
33 ]
34 ]
35 ];
36
37 $this->db_query_class = new WP_Query($args);
38
39 if (isset($this->db_query_class->posts[0])) {
40
41 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
42 }
43
44 add_filter('woocommerce_order_partially_refunded', [$this, 'orderRefund'], 10, 2);
45 }
46
47 /**
48 * @param $email_class
49 */
50 public function remove_woocommerce_emails($email_class)
51 {
52
53 remove_action('woocommerce_order_partially_refunded_notification', [$email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_partial']);
54 remove_action('woocommerce_order_fully_refunded_notification', [$email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_full']);
55 }
56
57 /**
58 * @param $refund_id
59 * @param $order_id
60 */
61 public function orderRefund($refund_id, $order_id)
62 {
63
64 $query = $this->db_query_class;
65 $order = wc_get_order($refund_id);
66 $email = get_option('admin_email');
67
68 if (isset($query->posts[0])) {
69 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
70
71 $replacements = [];
72 foreach ($order->get_items() as $item_id => $item) {
73
74 $id = $item['product_id'];
75 $product_name = $item['name'];
76 $item_qty = $item['quantity'];
77 $item_total = $item['total'];
78 $product = wc_get_product($id);
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 $attributes = [];
93 $meta_data = $item->get_meta_data();
94
95 foreach ($meta_data as $meta) {
96 // Check for product attribute (pa_ is the prefix for standard attributes)
97 if (strpos($meta->key, 'pa_') === 0) {
98 // Standard product attribute
99 $formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product));
100 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value
101
102 // Add the attribute to the list with HTML markup
103 $attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value);
104
105 } else {
106
107 $formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : '';
108 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : '';
109
110 // Add the custom meta to the list with HTML markup
111 $attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value);
112
113 }
114 }
115
116 $formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : '';
117
118 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes];
119 }
120
121 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
122
123 // Order details array for email
124 $details = $details = Utils::woocommerce_order_email_contents($order);
125
126 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
127
128 $currency_symbol_position = get_option('woocommerce_currency_pos');
129
130 if($currency_symbol_position == 'left') {
131
132 $message = Utils::adjust_price_structure($message);
133 $message = preg_replace_callback(
134 '/<span>(<del[^>]*>.*?<\/del>\s*<u[^>]*>.*?<\/u>)<\/span>\s*([\d.,]+)<\/bdi><\/span><\/span>/',
135 function ($matches) {
136 // Return only the del and ins tags without the extra price
137 return '<span>' . $matches[1] . '</span>';
138 },
139 $message
140 );
141
142 } else if($currency_symbol_position == 'left_space') {
143
144 $message = preg_replace_callback(
145 '/<span>(<del[^>]*>.*?<\/del>\s*<u[^>]*>.*?<\/u>)<\/span>\s*&nbsp;[\d.,]+<\/bdi><\/span><\/span>/',
146 function ($matches) {
147 // Return only the del and ins tags without the extra price
148 return '<span>' . $matches[1] . '</span>';
149 },
150 $message
151 );
152
153 $message = Utils::adjust_left_space_price_structure($message);
154
155 }
156 $to = $order->get_billing_email();
157
158 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
159 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
160 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("partially refunded", 'emailkit');
161 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
162 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
163 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", Your order#" . esc_attr($order_id) . " " . esc_html__("has been Partially refunded", 'emailkit') . " - " . $pre_header;
164
165 $headers = [
166 'From: ' . $email . "\r\n",
167 'Reply-To: ' . $email . "\r\n",
168 'Content-Type: text/html; charset=UTF-8',
169 ];
170
171 wp_mail($to, $subject, $message, $headers);
172
173
174 }
175 }
176 }
177