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

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

139 lines 5.2 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 FailedOrder
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' => 'Failed Order',
23 ),
24 array(
25 'key' => 'emailkit_template_status',
26 'value' => 'Active',
27 ),
28 ),
29 );
30
31
32
33 $this->db_query_class = new WP_Query($args);
34
35 if (isset($this->db_query_class->posts[0])) {
36 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
37 }
38
39 add_filter('woocommerce_order_status_pending_to_failed_notification', [$this, 'failedOrder'], 10, 2);
40 add_filter('woocommerce_order_status_on-hold_to_failed_notification', [$this, 'failedOrder'], 10, 2);
41 }
42
43 public function remove_woocommerce_emails($email_class)
44 {
45
46 remove_action('woocommerce_order_status_pending_to_failed_notification', array($email_class->emails['WC_Email_Failed_Order'], 'trigger'));
47 remove_action('woocommerce_order_status_on-hold_to_failed_notification', array($email_class->emails['WC_Email_Failed_Order'], 'trigger'));
48 }
49
50 public function failedOrder($order_id, $order)
51 {
52
53
54 $query = $this->db_query_class;
55 $email = get_option('admin_email');
56 if (isset($query->posts[0])) {
57 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
58
59 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
60
61 foreach ($order->get_items() as $item_id => $item) {
62 $product = $item->get_product();
63 $id = $item['product_id'];
64 $product_name = $item['name'];
65 $item_qty = $item['quantity'];
66 $item_total = $item['total'];
67 $product_price = $product->get_price();
68
69 // Use the currency symbol from the WooCommerce settings
70 $currency_symbol = $woocommerce_currency_settings;
71
72 // Format the product price with the currency symbol
73 $formatted_product_price = $currency_symbol . ' ' . number_format($product_price, 2);
74
75 // Format the item total with the currency symbol
76 $formatted_item_total = $currency_symbol . ' ' . number_format($item_total, 2);
77
78 $placeholders = ["{{product_name}}", "{{quantity}}", "{{total}}", "{{product_price}}"];
79 $replacements = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
80
81 }
82
83 $html = str_replace($placeholders, $replacements, $html);
84
85 $order = wc_get_order($order_id);
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
91 $details = [
92 "{{order_id}}" => $order->get_id(),
93 "{{order_number}}" => $order->get_order_number(),
94 "{{order_status}}" => $order->get_status(),
95 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
96 "{{order_currency}}" => $order->get_currency(),
97 "{{billing_phone}}" => $order->get_billing_phone(),
98 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
99 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
100 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
101 "{{shipping_method}}" => $order->get_shipping_method(),
102 "{{payment_method}}" => $order->get_payment_method_title(),
103 "{{total}}" => wc_format_decimal($order->get_total(), 2),
104 "{{billing_name}}" => $order->get_formatted_billing_full_name(),
105 "{{billing_first_name}}" => $order->get_billing_first_name(),
106 "{{billing_last_name}}" => $order->get_billing_last_name(),
107 "{{billing_company}}" => $order->get_billing_company(),
108 "{{billing_address_1}}" => $order->get_billing_address_1(),
109 "{{billing_address_2}}" => $order->get_billing_address_2(),
110 "{{billing_city}}" => $order->get_billing_city(),
111 "{{billing_state}}" => $order->get_billing_state(),
112 "{{billing_postcode}}" => $order->get_billing_postcode(),
113 "{{billing_country}}" => $order->get_billing_country(),
114 "{{billing_email}}" => $order->get_billing_email(),
115 "{{shipping_name}}" => $shipping_full_name,
116 "{{shipping_company}}" => $order->get_shipping_company(),
117 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
118 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
119 "{{shipping_city}}" => $order->get_shipping_city(),
120 "{{shipping_state}}" => $order->get_shipping_state(),
121 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
122 "{{shipping_country}}" => $order->get_shipping_country(),
123 "{{customer_note}}" => $order->get_customer_note(),
124 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
125 ];
126
127 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
128 $to = $order->get_billing_email();
129 $subject = esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", your order has been failed. order# " . esc_attr($order_id) . " on your store";
130 $headers = [
131 'From: ' . $email . "\r\n",
132 'Reply-To: ' . $email . "\r\n",
133 'Content-Type: text/html; charset=UTF-8'
134 ];
135
136 wp_mail($to, $subject, $message, $headers);
137 }
138 }
139 }