PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.2.0
EmailKit – Email Customizer for WooCommerce & WP v1.2.0
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.2.0, at includes/Admin/Emails/Woocommerce/InvoiceOrder.php

134 lines 4.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 WP_Query;
6
7 defined("ABSPATH") || exit;
8
9 class InvoiceOrder
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' => 'Customer Invoice',
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 add_filter('woocommerce_email_recipient_customer_invoice', [$this, 'invoiceEmail'], 10, 2);
39 }
40
41 public function remove_woocommerce_emails($email_class)
42 {
43
44 remove_action('woocommerce_email_recipient_customer_invoice', array($email_class->emails['WC_Email_Customer_Invoice'], 'trigger'));
45 }
46
47 public function invoiceEmail($order_id, $order)
48 {
49
50 $query = $this->db_query_class;
51 $email = get_option('admin_email');
52 if (isset($query->posts[0])) {
53 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
54
55 // Iterating through each WC_Order_Item_Product objects
56 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
57
58 foreach ($order->get_items() as $item_id => $item) {
59 $product = $item->get_product();
60 $id = $item['product_id'];
61 $product_name = $item['name'];
62 $item_qty = $item['quantity'];
63 $item_total = $item['total'];
64 $product_price = $product->get_price();
65
66 // Use the currency symbol from the WooCommerce settings
67 $currency_symbol = $woocommerce_currency_settings;
68
69 // Format the product price with the currency symbol
70 $formatted_product_price = $currency_symbol . ' ' . number_format($product_price, 2);
71
72 // Format the item total with the currency symbol
73 $formatted_item_total = $currency_symbol . ' ' . number_format($item_total, 2);
74
75 $placeholders = ["{{product_name}}", "{{quantity}}", "{{total}}", "{{product_price}}"];
76 $replacements = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
77
78 }
79
80 $html = str_replace($placeholders, $replacements, $html);
81
82 $shipping_first_name = $order->get_shipping_first_name();
83 $shipping_last_name = $order->get_shipping_last_name();
84 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
85
86 $details = [
87 "{{order_id}}" => $order_id,
88 "{{order_number}}" => $order->get_order_number(),
89 "{{order_status}}" => $order->get_status(),
90 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
91 "{{order_currency}}" => $order->get_currency(),
92 "{{billing_phone}}" => $order->get_billing_phone(),
93 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
94 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
95 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
96 "{{shipping_method}}" => $order->get_shipping_method(),
97 "{{payment_method}}" => $order->get_payment_method_title(),
98 "{{total}}" => wc_format_decimal($order->get_total(), 2),
99 "{{billing_name}}" => $order->get_formatted_billing_full_name(),
100 "{{billing_first_name}}" => $order->get_billing_first_name(),
101 "{{billing_last_name}}" => $order->get_billing_last_name(),
102 "{{billing_company}}" => $order->get_billing_company(),
103 "{{billing_address_1}}" => $order->get_billing_address_1(),
104 "{{billing_address_2}}" => $order->get_billing_address_2(),
105 "{{billing_city}}" => $order->get_billing_city(),
106 "{{billing_state}}" => $order->get_billing_state(),
107 "{{billing_postcode}}" => $order->get_billing_postcode(),
108 "{{billing_country}}" => $order->get_billing_country(),
109 "{{billing_email}}" => $order->get_billing_email(),
110 "{{shipping_name}}" => $shipping_full_name,
111 "{{shipping_company}}" => $order->get_shipping_company(),
112 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
113 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
114 "{{shipping_city}}" => $order->get_shipping_city(),
115 "{{shipping_state}}" => $order->get_shipping_state(),
116 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
117 "{{shipping_country}}" => $order->get_shipping_country(),
118 "{{customer_note}}" => $order->get_customer_note(),
119 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
120 ];
121
122 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
123 $to = $order->get_billing_email();
124 $subject = esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", Here is your product invoice";
125 $headers = [
126 'From: ' . $email . "\r\n",
127 'Reply-To: ' . $email . "\r\n",
128 'Content-Type: text/html; charset=UTF-8'
129 ];
130
131 wp_mail($to, $subject, $message, $headers);
132 }
133 }
134 }