| 1 |
<?php |
| 2 |
namespace EmailKit\Admin\Emails\Woocommerce; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
use EmailKit\Admin\Emails\EmailLists; |
| 6 |
use EmailKit\Admin\Emails\Helpers\Utils; |
| 7 |
|
| 8 |
defined("ABSPATH") || exit; |
| 9 |
|
| 10 |
class CompletedOrder |
| 11 |
{ |
| 12 |
|
| 13 |
private $db_query_class = null; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
|
| 18 |
$args = array( |
| 19 |
'post_type' => 'emailkit', |
| 20 |
'meta_query' => array( |
| 21 |
array( |
| 22 |
'key' => 'emailkit_template_type', |
| 23 |
'value' => EmailLists::COMPLETED_ORDER, |
| 24 |
), |
| 25 |
array( |
| 26 |
'key' => 'emailkit_template_status', |
| 27 |
'value' => 'Active', |
| 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_completed_notification', [$this, 'completeOrder'], 10, 2); |
| 40 |
} |
| 41 |
|
| 42 |
public function remove_woocommerce_emails($email_class) |
| 43 |
{ |
| 44 |
|
| 45 |
remove_action('woocommerce_order_status_completed_notification', array($email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger')); |
| 46 |
} |
| 47 |
|
| 48 |
public function completeOrder($order_id, $order) |
| 49 |
{ |
| 50 |
|
| 51 |
|
| 52 |
$query = $this->db_query_class; |
| 53 |
$email = get_option('admin_email'); |
| 54 |
if (isset($query->posts[0])) { |
| 55 |
$html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true); |
| 56 |
|
| 57 |
$replacements = []; |
| 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() * $item_qty; |
| 65 |
|
| 66 |
|
| 67 |
// Format the product price with the currency symbol |
| 68 |
$formatted_product_price = wc_price($product_price); |
| 69 |
|
| 70 |
// Format the item total with the currency symbol |
| 71 |
$formatted_item_total = wc_price($item_total); |
| 72 |
|
| 73 |
|
| 74 |
$product_image_id = $product->get_image_id(); |
| 75 |
$product_image_url = $product_image_id ? wp_get_attachment_url($product_image_id) : wc_placeholder_img_src(); |
| 76 |
|
| 77 |
$product_sku = $product->get_sku(); |
| 78 |
$attributes = []; |
| 79 |
$meta_data = $item->get_meta_data(); |
| 80 |
|
| 81 |
foreach ($meta_data as $meta) { |
| 82 |
// Check for product attribute (pa_ is the prefix for standard attributes) |
| 83 |
if (strpos($meta->key, 'pa_') === 0) { |
| 84 |
// Standard product attribute |
| 85 |
$formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product)); |
| 86 |
$formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value |
| 87 |
|
| 88 |
// Add the attribute to the list with HTML markup |
| 89 |
$attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value); |
| 90 |
|
| 91 |
} else { |
| 92 |
|
| 93 |
$formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : ''; |
| 94 |
$formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; |
| 95 |
|
| 96 |
// Add the custom meta to the list with HTML markup |
| 97 |
$attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value); |
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : ''; |
| 103 |
|
| 104 |
|
| 105 |
$replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes]; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
$html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements); |
| 110 |
|
| 111 |
$order = wc_get_order($order_id); |
| 112 |
|
| 113 |
// Order details array for email |
| 114 |
$details = $details = Utils::woocommerce_order_email_contents($order); |
| 115 |
|
| 116 |
$message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html)); |
| 117 |
|
| 118 |
$currency_symbol_position = get_option('woocommerce_currency_pos'); |
| 119 |
|
| 120 |
if($currency_symbol_position == 'left') { |
| 121 |
|
| 122 |
$message = Utils::adjust_price_structure($message); |
| 123 |
|
| 124 |
} else if($currency_symbol_position == 'left_space') { |
| 125 |
|
| 126 |
$message = Utils::adjust_left_space_price_structure($message); |
| 127 |
|
| 128 |
} |
| 129 |
$to = $order->get_billing_email(); |
| 130 |
|
| 131 |
$pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true); |
| 132 |
$pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template); |
| 133 |
$pre_header = !empty($pre_header) ? $pre_header : esc_html__(sprintf( 'order #%1$s is completed', $order_id ), "emailkit"); |
| 134 |
$subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true); |
| 135 |
$subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template); |
| 136 |
$subject = !empty($subject) ? $subject . ' - ' . $pre_header : 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") . ' - ' . $pre_header; |
| 137 |
|
| 138 |
$headers = [ |
| 139 |
'From: ' . $email . "\r\n", |
| 140 |
'Reply-To: ' . $email . "\r\n", |
| 141 |
'Content-Type: text/html; charset=UTF-8', |
| 142 |
'X-WPMAIL-PREHEADER: ' . $pre_header, |
| 143 |
]; |
| 144 |
|
| 145 |
wp_mail($to, $subject, $message, $headers); |
| 146 |
} |
| 147 |
} |
| 148 |
} |