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

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

156 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace EmailKit\Admin\Emails\Woocommerce;
3
4 use WP_Query;
5 use EmailKit\Admin\Emails\EmailLists;
6
7 defined("ABSPATH") || exit;
8
9 class RefundOrder
10 {
11
12 private $db_query_class = null;
13
14 public function __construct()
15 {
16
17 $args = array(
18 'post_type' => 'emailkit',
19 'meta_query' => array(
20 array(
21 'key' => 'emailkit_template_type',
22 'value' => EmailLists::REFUNDED_ORDER,
23 ),
24 array(
25 'key' => 'emailkit_template_status',
26 'value' => 'Active',
27 ),
28 ),
29 );
30
31
32 $this->db_query_class = new WP_Query($args);
33
34 if (isset($this->db_query_class->posts[0])) {
35 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
36 }
37
38
39 add_filter('woocommerce_order_status_refunded', [$this, 'orderRefund'], 10, 2);
40 }
41
42 public function remove_woocommerce_emails($email_class)
43 {
44
45 remove_action('woocommerce_order_fully_refunded_notification', array($email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_full'));
46 remove_action('woocommerce_order_partially_refunded_notification', array($email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_partial'));
47 }
48
49 public function orderRefund($order_id, $order)
50 {
51
52 $query = $this->db_query_class;
53 $email = get_option('admin_email');
54
55
56 if (isset($query->posts[0])) {
57 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
58
59 // Iterating through each WC_Order_Item_Product objects
60 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
61 $replacements = [];
62 $refunded_qty = 0;
63 foreach ($order->get_items() as $item_id => $item) {
64 $product = $item->get_product();
65 $id = $item['product_id'];
66 $product_name = $item['name'];
67 $item_qty = $item['quantity'];
68 $item_total = $item['total'];
69 $product_price = $product->get_price() * $item_qty;
70
71 // Use the currency symbol from the WooCommerce settings
72 $currency_symbol = $woocommerce_currency_settings;
73
74 // Format the product price with the currency symbol
75 $formatted_product_price = $currency_symbol . number_format($product_price, 2);
76
77 // Format the item total with the currency symbol
78 $formatted_item_total = $currency_symbol . number_format($item_total, 2);
79
80
81 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
82
83 }
84
85 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
86
87 $shipping_first_name = $order->get_shipping_first_name();
88 $shipping_last_name = $order->get_shipping_last_name();
89 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
90 $billing_first_name = $order->get_billing_first_name();
91 $billing_last_name = $order->get_billing_last_name();
92 $billing_full_name = $billing_first_name . ' ' . $billing_last_name;
93
94 $details = [
95 "{{order_id}}" => $order_id,
96 "{{order_number}}" => $order->get_order_number(),
97 "{{order_status}}" => $order->get_status(),
98 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
99 "{{refund_total}}" => wc_format_decimal($order->get_total_refunded(), 2),
100 "{{order_fully_refunded}}" => '-'.wc_price($order->get_total_refunded(), 2),
101 "{{order_total}}" => wc_price($order->get_total(), 2),
102 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
103 "{{order_currency}}" => $order->get_currency(),
104 "{{billing_phone}}" => $order->get_billing_phone(),
105 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
106 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
107 "{{shipping_method}}" => $order->get_shipping_method(),
108 "{{payment_method}}" => $order->get_payment_method_title(),
109 "{{total}}" => wc_price($order->get_total_refunded() - $order->get_total(), 2),
110 "{{billing_name}}" => $billing_full_name,
111 "{{billing_first_name}}" => $order->get_billing_first_name(),
112 "{{billing_last_name}}" => $order->get_billing_last_name(),
113 "{{billing_company}}" => $order->get_billing_company(),
114 "{{billing_address_1}}" => $order->get_billing_address_1(),
115 "{{billing_address_2}}" => $order->get_billing_address_2(),
116 "{{billing_city}}" => $order->get_billing_city(),
117 "{{billing_state}}" => $order->get_billing_state(),
118 "{{billing_postcode}}" => $order->get_billing_postcode(),
119 "{{billing_country}}" => $order->get_billing_country(),
120 "{{billing_email}}" => $order->get_billing_email(),
121 "{{shipping_name}}" => $shipping_full_name,
122 "{{shipping_first_name}}" => $shipping_first_name,
123 "{{shipping_last_name}}" => $shipping_last_name,
124 "{{shipping_company}}" => $order->get_shipping_company(),
125 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
126 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
127 "{{shipping_city}}" => $order->get_shipping_city(),
128 "{{shipping_state}}" => $order->get_shipping_state(),
129 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
130 "{{shipping_country}}" => $order->get_shipping_country(),
131 "{{shipping_phone}}" => $order->get_shipping_phone(),
132 "{{customer_note}}" => $order->get_customer_note(),
133 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
134 ];
135
136 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
137 $to = $order->get_billing_email();
138
139 $pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
140 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("Order has been refunded", "emailkit");
141 $subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true));
142 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", ".esc_html("Your order#") . esc_attr($order_id) . " " . esc_html__(" has been refunded", "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
155 }
156