| 1 |
<?php |
| 2 |
namespace EmailKit\Admin\Emails\Woocommerce; |
| 3 |
|
| 4 |
use WP_Query; |
| 5 |
|
| 6 |
defined("ABSPATH") || exit; |
| 7 |
|
| 8 |
class RefundOrder |
| 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' => 'Refunded 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 |
|
| 38 |
add_filter('woocommerce_order_status_refunded', [$this, 'orderRefund'], 10, 2); |
| 39 |
} |
| 40 |
|
| 41 |
public function remove_woocommerce_emails($email_class) |
| 42 |
{ |
| 43 |
|
| 44 |
remove_action('woocommerce_order_fully_refunded_notification', array($email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_full')); |
| 45 |
remove_action('woocommerce_order_partially_refunded_notification', array($email_class->emails['WC_Email_Customer_Refunded_Order'], 'trigger_partial')); |
| 46 |
} |
| 47 |
|
| 48 |
public function orderRefund($order_id, $order) |
| 49 |
{ |
| 50 |
|
| 51 |
$query = $this->db_query_class; |
| 52 |
$email = get_option('admin_email'); |
| 53 |
|
| 54 |
|
| 55 |
if (isset($query->posts[0])) { |
| 56 |
$html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true); |
| 57 |
|
| 58 |
// Iterating through each WC_Order_Item_Product objects |
| 59 |
$woocommerce_currency_settings = get_woocommerce_currency_symbol(); |
| 60 |
$replacements = []; |
| 61 |
$refunded_qty = 0; |
| 62 |
foreach ($order->get_items() as $item_id => $item) { |
| 63 |
$product = $item->get_product(); |
| 64 |
$id = $item['product_id']; |
| 65 |
$product_name = $item['name']; |
| 66 |
$item_qty = $item['quantity']; |
| 67 |
$item_total = $item['total']; |
| 68 |
$product_price = $product->get_price() * $item_qty; |
| 69 |
|
| 70 |
// Use the currency symbol from the WooCommerce settings |
| 71 |
$currency_symbol = $woocommerce_currency_settings; |
| 72 |
|
| 73 |
// Format the product price with the currency symbol |
| 74 |
$formatted_product_price = $currency_symbol . number_format($product_price, 2); |
| 75 |
|
| 76 |
// Format the item total with the currency symbol |
| 77 |
$formatted_item_total = $currency_symbol . number_format($item_total, 2); |
| 78 |
|
| 79 |
|
| 80 |
$replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price]; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
$html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements); |
| 85 |
|
| 86 |
$shipping_first_name = $order->get_shipping_first_name(); |
| 87 |
$shipping_last_name = $order->get_shipping_last_name(); |
| 88 |
$shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name; |
| 89 |
$billing_first_name = $order->get_billing_first_name(); |
| 90 |
$billing_last_name = $order->get_billing_last_name(); |
| 91 |
$billing_full_name = $billing_first_name . ' ' . $billing_last_name; |
| 92 |
|
| 93 |
$details = [ |
| 94 |
"{{order_id}}" => $order_id, |
| 95 |
"{{order_number}}" => $order->get_order_number(), |
| 96 |
"{{order_status}}" => $order->get_status(), |
| 97 |
"{{shipping_total}}" => wc_price( $order->get_shipping_total() ), |
| 98 |
"{{refund_total}}" => wc_format_decimal($order->get_total_refunded(), 2), |
| 99 |
"{{order_fully_refunded}}" => '-'.wc_price($order->get_total_refunded(), 2), |
| 100 |
"{{order_total}}" => wc_price($order->get_total(), 2), |
| 101 |
"{{order_subtotal}}" => wc_price( $order->get_subtotal()), |
| 102 |
"{{order_currency}}" => $order->get_currency(), |
| 103 |
"{{billing_phone}}" => $order->get_billing_phone(), |
| 104 |
"{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2), |
| 105 |
"{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)), |
| 106 |
"{{shipping_method}}" => $order->get_shipping_method(), |
| 107 |
"{{payment_method}}" => $order->get_payment_method_title(), |
| 108 |
"{{total}}" => wc_price($order->get_total_refunded() - $order->get_total(), 2), |
| 109 |
"{{billing_name}}" => $billing_full_name, |
| 110 |
"{{billing_first_name}}" => $order->get_billing_first_name(), |
| 111 |
"{{billing_last_name}}" => $order->get_billing_last_name(), |
| 112 |
"{{billing_company}}" => $order->get_billing_company(), |
| 113 |
"{{billing_address_1}}" => $order->get_billing_address_1(), |
| 114 |
"{{billing_address_2}}" => $order->get_billing_address_2(), |
| 115 |
"{{billing_city}}" => $order->get_billing_city(), |
| 116 |
"{{billing_state}}" => $order->get_billing_state(), |
| 117 |
"{{billing_postcode}}" => $order->get_billing_postcode(), |
| 118 |
"{{billing_country}}" => $order->get_billing_country(), |
| 119 |
"{{billing_email}}" => $order->get_billing_email(), |
| 120 |
"{{shipping_name}}" => $shipping_full_name, |
| 121 |
"{{shipping_company}}" => $order->get_shipping_company(), |
| 122 |
"{{shipping_address_1}}" => $order->get_shipping_address_1(), |
| 123 |
"{{shipping_address_2}}" => $order->get_shipping_address_2(), |
| 124 |
"{{shipping_city}}" => $order->get_shipping_city(), |
| 125 |
"{{shipping_state}}" => $order->get_shipping_state(), |
| 126 |
"{{shipping_postcode}}" => $order->get_shipping_postcode(), |
| 127 |
"{{shipping_country}}" => $order->get_shipping_country(), |
| 128 |
"{{customer_note}}" => $order->get_customer_note(), |
| 129 |
"{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0, |
| 130 |
]; |
| 131 |
|
| 132 |
$message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html)); |
| 133 |
$to = $order->get_billing_email(); |
| 134 |
$subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true)); |
| 135 |
$subject = !empty($subject) ? $subject : esc_attr($order->get_billing_first_name() . " " . $order->get_billing_last_name()) . ", ".esc_html("Your order#") . esc_attr($order_id) . " " . esc_html__(" has been refunded", "emailkit"); |
| 136 |
|
| 137 |
$pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true); |
| 138 |
$pre_header = !empty($pre_header) ? $pre_header : esc_html("Your order#") . esc_attr($order_id) . " " . esc_html__("has been refunded", "emailkit"); |
| 139 |
|
| 140 |
$headers = [ |
| 141 |
'From: ' . $email . "\r\n", |
| 142 |
'Reply-To: ' . $email . "\r\n", |
| 143 |
'Content-Type: text/html; charset=UTF-8', |
| 144 |
'X-WPMAIL-PREHEADER: ' . $pre_header, |
| 145 |
]; |
| 146 |
|
| 147 |
wp_mail($to, $subject, $message, $headers); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|