| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Emails\Woocommerce; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
|
| 7 |
defined("ABSPATH") || exit; |
| 8 |
|
| 9 |
class CustomerNote |
| 10 |
{ |
| 11 |
|
| 12 |
private $db_query_class = null; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$args = [ |
| 17 |
'post_type' => 'emailkit', |
| 18 |
'meta_query' => [ |
| 19 |
[ |
| 20 |
'key' => 'emailkit_template_type', |
| 21 |
'value' => 'Customer Note', |
| 22 |
], |
| 23 |
[ |
| 24 |
'key' => 'emailkit_template_status', |
| 25 |
'value' => 'Active', |
| 26 |
], |
| 27 |
], |
| 28 |
]; |
| 29 |
|
| 30 |
$this->db_query_class = new WP_Query($args); |
| 31 |
|
| 32 |
if (isset($this->db_query_class->posts[0])) { |
| 33 |
add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']); |
| 34 |
} |
| 35 |
|
| 36 |
add_filter('woocommerce_new_customer_note_notification', [$this, 'noteCustomer'], 10, 1); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param $email_class |
| 41 |
*/ |
| 42 |
public function remove_woocommerce_emails($email_class) |
| 43 |
{ |
| 44 |
|
| 45 |
remove_action('woocommerce_new_customer_note_notification', [$email_class->emails['WC_Email_Customer_Note'], 'trigger']); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param $order |
| 50 |
*/ |
| 51 |
public function noteCustomer($order) |
| 52 |
{ |
| 53 |
|
| 54 |
|
| 55 |
$query = $this->db_query_class; |
| 56 |
$email = get_option('admin_email'); |
| 57 |
|
| 58 |
if (isset($query->posts[0])) { |
| 59 |
|
| 60 |
if (!empty($order)) { |
| 61 |
$defaults = [ |
| 62 |
'order_id' => '', |
| 63 |
'customer_note' => '' |
| 64 |
]; |
| 65 |
|
| 66 |
$order = wp_parse_args($order, $defaults); |
| 67 |
|
| 68 |
$order_id = $order['order_id']; |
| 69 |
$customer_note = $order['customer_note']; |
| 70 |
} |
| 71 |
if ($order_id) { |
| 72 |
$order = wc_get_order($order_id); |
| 73 |
$html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true); |
| 74 |
|
| 75 |
$woocommerce_currency_settings = get_woocommerce_currency_symbol(); |
| 76 |
$replacements = []; |
| 77 |
foreach ($order->get_items() as $item_id => $item) { |
| 78 |
|
| 79 |
$id = $item['product_id']; |
| 80 |
$product_name = $item['name']; |
| 81 |
$item_qty = $item['quantity']; |
| 82 |
$item_total = $item['total']; |
| 83 |
$product = wc_get_product($id); |
| 84 |
$product_price = $product->get_price() * $item_qty; |
| 85 |
|
| 86 |
// Use the currency symbol from the WooCommerce settings |
| 87 |
$currency_symbol = $woocommerce_currency_settings; |
| 88 |
|
| 89 |
// Format the product price with the currency symbol |
| 90 |
$formatted_product_price = $currency_symbol . number_format($product_price, 2); |
| 91 |
|
| 92 |
// Format the item total with the currency symbol |
| 93 |
$formatted_item_total = $currency_symbol . number_format($item_total, 2); |
| 94 |
|
| 95 |
|
| 96 |
$replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price]; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
$html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements); |
| 101 |
|
| 102 |
$shipping_first_name = $order->get_shipping_first_name(); |
| 103 |
$shipping_last_name = $order->get_shipping_last_name(); |
| 104 |
$shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name; |
| 105 |
|
| 106 |
$details = [ |
| 107 |
"{{order_id}}" => $order_id, |
| 108 |
"{{customer_note}}" => $customer_note, |
| 109 |
"{{order_number}}" => $order->get_order_number(), |
| 110 |
"{{order_status}}" => $order->get_status(), |
| 111 |
"{{order_subtotal}}" => wc_price( $order->get_subtotal()), |
| 112 |
"{{order_currency}}" => $order->get_currency(), |
| 113 |
"{{billing_phone}}" => $order->get_billing_phone(), |
| 114 |
"{{shipping_total}}" => wc_price( $order->get_shipping_total() ), |
| 115 |
"{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2), |
| 116 |
"{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)), |
| 117 |
"{{shipping_method}}" => $order->get_shipping_method(), |
| 118 |
"{{payment_method}}" => $order->get_payment_method_title(), |
| 119 |
"{{total}}" => wc_price($order->get_total(), 2), |
| 120 |
"{{billing_name}}" => $order->get_formatted_billing_full_name(), |
| 121 |
"{{billing_first_name}}" => $order->get_billing_first_name(), |
| 122 |
"{{billing_last_name}}" => $order->get_billing_last_name(), |
| 123 |
"{{billing_company}}" => $order->get_billing_company(), |
| 124 |
"{{billing_address_1}}" => $order->get_billing_address_1(), |
| 125 |
"{{billing_address_2}}" => $order->get_billing_address_2(), |
| 126 |
"{{billing_city}}" => $order->get_billing_city(), |
| 127 |
"{{billing_state}}" => $order->get_billing_state(), |
| 128 |
"{{billing_postcode}}" => $order->get_billing_postcode(), |
| 129 |
"{{billing_country}}" => $order->get_billing_country(), |
| 130 |
"{{billing_email}}" => $order->get_billing_email(), |
| 131 |
"{{shipping_name}}" => $shipping_full_name, |
| 132 |
"{{shipping_company}}" => $order->get_shipping_company(), |
| 133 |
"{{shipping_address_1}}" => $order->get_shipping_address_1(), |
| 134 |
"{{shipping_address_2}}" => $order->get_shipping_address_2(), |
| 135 |
"{{shipping_city}}" => $order->get_shipping_city(), |
| 136 |
"{{shipping_state}}" => $order->get_shipping_state(), |
| 137 |
"{{shipping_postcode}}" => $order->get_shipping_postcode(), |
| 138 |
"{{shipping_country}}" => $order->get_shipping_country(), |
| 139 |
"{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0, |
| 140 |
]; |
| 141 |
|
| 142 |
$message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html)); |
| 143 |
|
| 144 |
$to = $order->get_billing_email(); |
| 145 |
$order_date = $order->get_date_created(); |
| 146 |
$formatted_date = esc_attr($order_date->date('F j, Y')); |
| 147 |
$subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true)); |
| 148 |
$subject = !empty($subject) ? $subject : esc_html__("Note added to your order on ", 'emailkit') . $formatted_date ." " . esc_html__("on", "emailkit"). " " . esc_attr(get_bloginfo('name')); |
| 149 |
|
| 150 |
$pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true); |
| 151 |
$pre_header = !empty($pre_header) ? $pre_header : esc_html__("Note added to your order on ", 'emailkit') . $formatted_date ." " . esc_html__("on", "emailkit"). " " . esc_attr(get_bloginfo('name')); |
| 152 |
|
| 153 |
$headers = [ |
| 154 |
'From: ' . $email . "\r\n", |
| 155 |
'Reply-To: ' . $email . "\r\n", |
| 156 |
'Content-Type: text/html; charset=UTF-8', |
| 157 |
'X-WPMAIL-PREHEADER: ' . $pre_header, |
| 158 |
]; |
| 159 |
|
| 160 |
wp_mail($to, $subject, $message, $headers); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|