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

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

166 lines 7.0 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
8 defined('ABSPATH') || exit;
9
10 class PartialRefund
11 {
12
13 /**
14 * @var mixed
15 */
16 private $db_query_class = null;
17
18 public function __construct()
19 {
20
21
22 $args = [
23 'post_type' => 'emailkit',
24 'meta_query' => [
25 [
26 'key' => 'emailkit_template_type',
27 'value' => EmailLists::PARTIAL_REFUND
28 ],
29 [
30 'key' => 'emailkit_template_status',
31 'value' => 'Active'
32 ]
33 ]
34 ];
35
36 $this->db_query_class = new WP_Query($args);
37
38 if (isset($this->db_query_class->posts[0])) {
39
40 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
41 }
42
43 add_filter('woocommerce_order_partially_refunded', [$this, 'orderRefund'], 10, 2);
44 }
45
46 /**
47 * @param $email_class
48 */
49 public function remove_woocommerce_emails($email_class)
50 {
51
52 remove_action('woocommerce_order_partially_refunded_notification', [$email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_partial']);
53 remove_action('woocommerce_order_fully_refunded_notification', [$email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_full']);
54 }
55
56 /**
57 * @param $refund_id
58 * @param $order_id
59 */
60 public function orderRefund($refund_id, $order_id)
61 {
62
63 $query = $this->db_query_class;
64 $order = wc_get_order($refund_id);
65 $email = get_option('admin_email');
66
67 if (isset($query->posts[0])) {
68 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
69
70 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
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 // Use the currency symbol from the WooCommerce settings
83 $currency_symbol = $woocommerce_currency_settings;
84
85 // Format the product price with the currency symbol
86 $formatted_product_price = $currency_symbol . number_format($product_price, 2);
87
88 // Format the item total with the currency symbol
89 $formatted_item_total = $currency_symbol . number_format($item_total, 2);
90
91 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
92 }
93
94 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
95
96 $shipping_first_name = $order->get_shipping_first_name();
97 $shipping_last_name = $order->get_shipping_last_name();
98 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
99 $billing_first_name = $order->get_billing_first_name();
100 $billing_last_name = $order->get_billing_last_name();
101 $billing_full_name = $billing_first_name . ' ' . $billing_last_name;
102
103 $details = [
104 "{{order_id}}" => $order_id,
105 "{{order_number}}" => $order->get_order_number(),
106 "{{order_status}}" => $order->get_status(),
107 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
108 "{{partial_refund_amount}}" => '-'. wc_price($order->get_total_refunded(), 2),
109 "{{remaining_refund_amount}}" => '-'.$order->get_remaining_refund_amount(),
110 "{{order_total}}" => wc_price($order->get_total(), 2),
111 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
112 "{{order_currency}}" => $order->get_currency(),
113 "{{billing_phone}}" => $order->get_billing_phone(),
114 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
115 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
116 "{{shipping_method}}" => $order->get_shipping_method(),
117 "{{payment_method}}" => $order->get_payment_method_title(),
118 "{{total}}" => '<del aria-hidden="true">' . wc_price($order->get_subtotal()) . '</del>' . ' ' . '<u>' . wc_price($order->get_subtotal() - $order->get_total_refunded()) . '</u>',
119 "{{billing_name}}" => $billing_full_name,
120 "{{billing_first_name}}" => $order->get_billing_first_name(),
121 "{{billing_last_name}}" => $order->get_billing_last_name(),
122 "{{billing_company}}" => $order->get_billing_company(),
123 "{{billing_address_1}}" => $order->get_billing_address_1(),
124 "{{billing_address_2}}" => $order->get_billing_address_2(),
125 "{{billing_city}}" => $order->get_billing_city(),
126 "{{billing_state}}" => $order->get_billing_state(),
127 "{{billing_postcode}}" => $order->get_billing_postcode(),
128 "{{billing_country}}" => $order->get_billing_country(),
129 "{{billing_email}}" => $order->get_billing_email(),
130 "{{shipping_name}}" => $shipping_full_name,
131 "{{shipping_first_name}}" => $shipping_first_name,
132 "{{shipping_last_name}}" => $shipping_last_name,
133 "{{shipping_company}}" => $order->get_shipping_company(),
134 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
135 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
136 "{{shipping_city}}" => $order->get_shipping_city(),
137 "{{shipping_state}}" => $order->get_shipping_state(),
138 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
139 "{{shipping_country}}" => $order->get_shipping_country(),
140 "{{shipping_phone}}" => $order->get_shipping_phone(),
141 "{{customer_note}}" => $order->get_customer_note(),
142 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
143 "{{site_name}}" => get_bloginfo('name'),
144 ];
145
146 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
147 $to = $order->get_billing_email();
148
149 $pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
150 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("partially refunded", 'emailkit');
151 $subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true));
152 $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;
153
154 $headers = [
155 'From: ' . $email . "\r\n",
156 'Reply-To: ' . $email . "\r\n",
157 'Content-Type: text/html; charset=UTF-8',
158 ];
159
160 wp_mail($to, $subject, $message, $headers);
161
162
163 }
164 }
165 }
166