PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.9
EmailKit – Email Customizer for WooCommerce & WP v1.6.9
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 / InvoiceOrder.php

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

166 lines 5.8 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 InvoiceOrder
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::CUSTOMER_INVOICE_OR_ORDER_DETAILS,
25 ),
26 array(
27 'key' => 'emailkit_template_status',
28 'value' => 'Active',
29 ),
30 ),
31 );
32
33
34 $this->db_query_class = new WP_Query($args);
35
36 // Only take over WooCommerce's "Customer invoice / Order details" email when an
37 // EmailKit template for it is actually active. Otherwise WooCommerce must be left
38 // untouched, so that "Send order details to customer" keeps working on its own.
39 if (isset($this->db_query_class->posts[0])) {
40 add_action('woocommerce_before_resend_order_emails', [$this, 'maybe_send_invoice_email'], 10, 2);
41 }
42 }
43
44 /**
45 * Fires from both of WooCommerce's invoice trigger paths:
46 * - classic order screen: WC_Meta_Box_Order_Actions::save() ('send_order_details')
47 * - new order screen: OrderActionsRestController::send_order_details()
48 *
49 * @param \WC_Order $order
50 * @param string $email_to_send
51 */
52 public function maybe_send_invoice_email($order, $email_to_send = '')
53 {
54
55 if ('customer_invoice' !== $email_to_send || !is_a($order, 'WC_Order')) {
56 return;
57 }
58
59 // Suppress WooCommerce's own copy for this request only, so the customer does not
60 // receive both. Scoped to this send, never registered globally.
61 add_filter('woocommerce_email_recipient_customer_invoice', '__return_empty_string', 999);
62
63 $this->invoiceEmail($order->get_id(), $order);
64 }
65
66 public function invoiceEmail($order_id, $order)
67 {
68
69 $query = $this->db_query_class;
70 $email = get_option('admin_email');
71 if (isset($query->posts[0]) && is_a($order, 'WC_Order')) {
72 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
73
74 $replacements = [];
75 foreach ($order->get_items() as $item_id => $item) {
76 $product = $item->get_product();
77 $id = $item['product_id'];
78 $product_name = $item['name'];
79 $item_qty = $item['quantity'];
80 $item_total = $item['total'];
81 $product_price = $product->get_price() * $item_qty;
82
83
84 // Format the product price with the currency symbol
85 $formatted_product_price = wc_price($product_price);
86
87 // Format the item total with the currency symbol
88 $formatted_item_total = wc_price($item_total);
89
90 $product_image_id = $product->get_image_id();
91 $product_image_url = $product_image_id ? wp_get_attachment_url($product_image_id) : wc_placeholder_img_src();
92
93 $product_sku = $product->get_sku();
94 $attributes = [];
95 $meta_data = $item->get_meta_data();
96
97 foreach ($meta_data as $meta) {
98 // Check for product attribute (pa_ is the prefix for standard attributes)
99 if (strpos($meta->key, 'pa_') === 0) {
100 // Standard product attribute
101 $formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product));
102 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value
103
104 // Add the attribute to the list with HTML markup
105 $attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value);
106
107 } else {
108
109 $formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : '';
110 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : '';
111
112 // Add the custom meta to the list with HTML markup
113 $attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value);
114
115 }
116 }
117
118 $formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : '';
119
120
121 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes];
122
123 }
124
125 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
126
127
128 // Order details array for email
129 $details = Utils::woocommerce_order_email_contents($order);
130
131 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
132
133 $currency_symbol_position = get_option('woocommerce_currency_pos');
134
135 if($currency_symbol_position == 'left') {
136
137 $message = Utils::adjust_price_structure($message);
138
139 } else if($currency_symbol_position == 'left_space') {
140
141 $message = Utils::adjust_left_space_price_structure($message);
142
143 }
144
145 $to = $order->get_billing_email();
146
147 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
148 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
149 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("product details", "emailkit");
150 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
151 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
152 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", " ." ".esc_html__("Here is your product invoice", "emailkit") . ' - ' . $pre_header;
153
154
155
156
157 $headers = [
158 'From: ' . $email . "\r\n",
159 'Reply-To: ' . $email . "\r\n",
160 'Content-Type: text/html; charset=UTF-8',
161 ];
162
163 wp_mail($to, $subject, $message, $headers);
164 }
165 }
166 }