PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.5
EmailKit – Email Customizer for WooCommerce & WP v1.4.5
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 / CompletedOrder.php

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

142 lines 5.4 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
6 defined("ABSPATH") || exit;
7
8 class CompletedOrder
9 {
10
11 private $db_query_class = null;
12
13 public function __construct()
14 {
15
16 $args = array(
17 'post_type' => 'emailkit',
18 'meta_query' => array(
19 array(
20 'key' => 'emailkit_template_type',
21 'value' => 'Completed Order',
22 ),
23 array(
24 'key' => 'emailkit_template_status',
25 'value' => 'Active',
26 ),
27 ),
28 );
29
30
31 $this->db_query_class = new WP_Query($args);
32
33 if (isset($this->db_query_class->posts[0])) {
34 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
35 }
36
37 add_filter('woocommerce_order_status_completed_notification', [$this, 'completeOrder'], 10, 2);
38 }
39
40 public function remove_woocommerce_emails($email_class)
41 {
42
43 remove_action('woocommerce_order_status_completed_notification', array($email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger'));
44 }
45
46 public function completeOrder($order_id, $order)
47 {
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 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
56 $replacements = [];
57 foreach ($order->get_items() as $item_id => $item) {
58 $product = $item->get_product();
59 $id = $item['product_id'];
60 $product_name = $item['name'];
61 $item_qty = $item['quantity'];
62 $item_total = $item['total'];
63 $product_price = $product->get_price() * $item_qty;
64
65 // Use the currency symbol from the WooCommerce settings
66 $currency_symbol = $woocommerce_currency_settings;
67
68 // Format the product price with the currency symbol
69 $formatted_product_price = $currency_symbol . number_format($product_price, 2);
70
71 // Format the item total with the currency symbol
72 $formatted_item_total = $currency_symbol . number_format($item_total, 2);
73
74
75 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
76
77 }
78
79 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
80
81 $order = wc_get_order($order_id);
82
83 $shipping_first_name = $order->get_shipping_first_name();
84 $shipping_last_name = $order->get_shipping_last_name();
85 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
86
87 $details = [
88 "{{order_id}}" => $order->get_id(),
89 "{{order_number}}" => $order->get_order_number(),
90 "{{order_status}}" => $order->get_status(),
91 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
92 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
93 "{{order_currency}}" => $order->get_currency(),
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_price($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 "{{billing_phone}}" => $order->get_billing_phone(),
111 "{{shipping_first_name}}" => $order->get_shipping_first_name(),
112 "{{shipping_name}}" => $shipping_full_name,
113 "{{shipping_company}}" => $order->get_shipping_company(),
114 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
115 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
116 "{{shipping_city}}" => $order->get_shipping_city(),
117 "{{shipping_state}}" => $order->get_shipping_state(),
118 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
119 "{{shipping_country}}" => $order->get_shipping_country(),
120 "{{customer_note}}" => $order->get_customer_note(),
121 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
122 ];
123
124 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
125 $to = $order->get_billing_email();
126 $subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true));
127 $subject = !empty($subject) ? $subject : esc_html__("Hi", "emailkit") . esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", " . esc_attr("Your order #$order_id is now complete");
128
129 $pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
130 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("Your order #$order_id is now complete", "emailkit");
131
132 $headers = [
133 'From: ' . $email . "\r\n",
134 'Reply-To: ' . $email . "\r\n",
135 'Content-Type: text/html; charset=UTF-8',
136 'X-WPMAIL-PREHEADER: ' . $pre_header,
137 ];
138
139 wp_mail($to, $subject, $message, $headers);
140 }
141 }
142 }