class-wc-email-cancelled-order.php
7 months ago
class-wc-email-customer-cancelled-order.php
7 months ago
class-wc-email-customer-completed-order.php
7 months ago
class-wc-email-customer-failed-order.php
7 months ago
class-wc-email-customer-fulfillment-created.php
5 months ago
class-wc-email-customer-fulfillment-deleted.php
5 months ago
class-wc-email-customer-fulfillment-updated.php
5 months ago
class-wc-email-customer-invoice.php
7 months ago
class-wc-email-customer-new-account.php
7 months ago
class-wc-email-customer-note.php
7 months ago
class-wc-email-customer-on-hold-order.php
7 months ago
class-wc-email-customer-pos-completed-order.php
7 months ago
class-wc-email-customer-pos-refunded-order.php
7 months ago
class-wc-email-customer-processing-order.php
7 months ago
class-wc-email-customer-refunded-order.php
7 months ago
class-wc-email-customer-reset-password.php
7 months ago
class-wc-email-failed-order.php
7 months ago
class-wc-email-new-order.php
7 months ago
class-wc-email.php
5 months ago
class-wc-email-customer-pos-completed-order.php
468 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Email_Customer_POS_Completed_Order file. |
| 4 | * |
| 5 | * @package WooCommerce\Emails |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | use Automattic\WooCommerce\Internal\Email\OrderPriceFormatter; |
| 13 | use Automattic\WooCommerce\Internal\Orders\PointOfSaleOrderUtil; |
| 14 | use Automattic\WooCommerce\Internal\Settings\PointOfSaleDefaultSettings; |
| 15 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 16 | |
| 17 | if ( ! class_exists( 'WC_Email_Customer_POS_Completed_Order', false ) ) : |
| 18 | |
| 19 | /** |
| 20 | * Customer Completed Order Email. |
| 21 | * |
| 22 | * Order complete emails are sent to the customer when the order is marked complete and usual indicates that the order has been shipped. |
| 23 | * |
| 24 | * @class WC_Email_Customer_POS_Completed_Order |
| 25 | * @version 2.0.0 |
| 26 | * @package WooCommerce\Classes\Emails |
| 27 | * @extends WC_Email |
| 28 | */ |
| 29 | class WC_Email_Customer_POS_Completed_Order extends WC_Email { |
| 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->id = 'customer_pos_completed_order'; |
| 36 | $this->customer_email = true; |
| 37 | $this->title = __( 'POS completed order', 'woocommerce' ); |
| 38 | $this->email_group = 'payments'; |
| 39 | $this->template_html = 'emails/customer-pos-completed-order.php'; |
| 40 | $this->template_plain = 'emails/plain/customer-pos-completed-order.php'; |
| 41 | $this->placeholders = array( |
| 42 | '{order_date}' => '', |
| 43 | '{order_number}' => '', |
| 44 | ); |
| 45 | |
| 46 | $this->enable_order_email_actions_for_pos_orders(); |
| 47 | |
| 48 | // Call parent constructor. |
| 49 | parent::__construct(); |
| 50 | |
| 51 | // Must be after parent's constructor which sets `email_improvements_enabled` property. |
| 52 | $this->description = $this->email_improvements_enabled |
| 53 | ? __( 'Let customers know once their POS order is complete.', 'woocommerce' ) |
| 54 | : __( 'Order complete emails are sent to customers when their POS orders are marked completed.', 'woocommerce' ); |
| 55 | |
| 56 | $this->manual = true; |
| 57 | |
| 58 | if ( $this->block_email_editor_enabled ) { |
| 59 | $this->title = __( 'POS order complete', 'woocommerce' ); |
| 60 | $this->description = __( 'Notifies customers when their in-person (POS) order has been completed.', 'woocommerce' ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Trigger the sending of this email. |
| 66 | * |
| 67 | * @param int $order_id The order ID. |
| 68 | * @param string $template_id The email template ID. |
| 69 | */ |
| 70 | public function trigger( $order_id, $template_id ) { |
| 71 | if ( $this->id !== $template_id ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $this->setup_locale(); |
| 76 | |
| 77 | if ( $order_id ) { |
| 78 | $order = wc_get_order( $order_id ); |
| 79 | } |
| 80 | |
| 81 | if ( is_a( $order, 'WC_Order' ) ) { |
| 82 | $this->object = $order; |
| 83 | $this->recipient = $this->object->get_billing_email(); |
| 84 | $this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() ); |
| 85 | $this->placeholders['{order_number}'] = $this->object->get_order_number(); |
| 86 | } |
| 87 | |
| 88 | if ( $this->get_recipient() ) { |
| 89 | $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 90 | } |
| 91 | |
| 92 | $this->restore_locale(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get email subject. |
| 97 | * |
| 98 | * @since 3.1.0 |
| 99 | * @return string |
| 100 | */ |
| 101 | public function get_default_subject() { |
| 102 | $store_name = $this->get_pos_store_name(); |
| 103 | /* translators: %1$s: Order number, %2$s: Store name */ |
| 104 | return sprintf( __( 'Your in-store purchase #%1$s at %2$s', 'woocommerce' ), '{order_number}', esc_html( $store_name ) ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get email heading. |
| 109 | * |
| 110 | * @since 3.1.0 |
| 111 | * @return string |
| 112 | */ |
| 113 | public function get_default_heading() { |
| 114 | return __( 'Thank you for your in-store purchase', 'woocommerce' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get content html. |
| 119 | * |
| 120 | * @return string |
| 121 | */ |
| 122 | public function get_content_html() { |
| 123 | $this->add_pos_customizations(); |
| 124 | add_action( 'woocommerce_pos_email_header', array( $this, 'email_header' ) ); |
| 125 | add_action( 'woocommerce_pos_email_footer', array( $this, 'email_footer' ) ); |
| 126 | $content = wc_get_template_html( |
| 127 | $this->template_html, |
| 128 | array( |
| 129 | 'order' => $this->object, |
| 130 | 'email_heading' => $this->get_heading(), |
| 131 | 'additional_content' => $this->get_additional_content(), |
| 132 | 'pos_store_name' => $this->get_pos_store_name(), |
| 133 | 'pos_store_email' => $this->get_pos_store_email(), |
| 134 | 'pos_store_phone_number' => $this->get_pos_store_phone_number(), |
| 135 | 'pos_store_address' => $this->get_pos_store_address(), |
| 136 | 'pos_refund_returns_policy' => $this->get_pos_refund_returns_policy(), |
| 137 | 'sent_to_admin' => false, |
| 138 | 'plain_text' => false, |
| 139 | 'email' => $this, |
| 140 | ) |
| 141 | ); |
| 142 | $this->remove_pos_customizations(); |
| 143 | remove_action( 'woocommerce_pos_email_header', array( $this, 'email_header' ) ); |
| 144 | remove_action( 'woocommerce_pos_email_footer', array( $this, 'email_footer' ) ); |
| 145 | return $content; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get content plain. |
| 150 | * |
| 151 | * @return string |
| 152 | */ |
| 153 | public function get_content_plain() { |
| 154 | $this->add_pos_customizations(); |
| 155 | $content = wc_get_template_html( |
| 156 | $this->template_plain, |
| 157 | array( |
| 158 | 'order' => $this->object, |
| 159 | 'email_heading' => $this->get_heading(), |
| 160 | 'additional_content' => $this->get_additional_content(), |
| 161 | 'pos_store_name' => $this->get_pos_store_name(), |
| 162 | 'pos_store_email' => $this->get_pos_store_email(), |
| 163 | 'pos_store_phone_number' => $this->get_pos_store_phone_number(), |
| 164 | 'pos_store_address' => $this->get_pos_store_address(), |
| 165 | 'pos_refund_returns_policy' => $this->get_pos_refund_returns_policy(), |
| 166 | 'sent_to_admin' => false, |
| 167 | 'plain_text' => true, |
| 168 | 'email' => $this, |
| 169 | ) |
| 170 | ); |
| 171 | $this->remove_pos_customizations(); |
| 172 | return $content; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get block editor email template content. |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public function get_block_editor_email_template_content() { |
| 181 | $this->add_pos_customizations(); |
| 182 | return wc_get_template_html( |
| 183 | $this->template_block_content, |
| 184 | array( |
| 185 | 'order' => $this->object, |
| 186 | 'sent_to_admin' => false, |
| 187 | 'plain_text' => false, |
| 188 | 'email' => $this, |
| 189 | ) |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Enable order email actions for POS orders. |
| 195 | */ |
| 196 | private function enable_order_email_actions_for_pos_orders() { |
| 197 | $this->enable_email_template_for_pos_orders(); |
| 198 | // Enable send email when requested. |
| 199 | add_action( 'woocommerce_rest_order_actions_email_send', array( $this, 'trigger' ), 10, 2 ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Override settings form fields to remove the enabled/disabled field as the email is manually sent. |
| 204 | */ |
| 205 | public function init_form_fields() { |
| 206 | /* translators: %s: list of placeholders */ |
| 207 | $placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' ); |
| 208 | $this->form_fields = array( |
| 209 | 'subject' => array( |
| 210 | 'title' => __( 'Subject', 'woocommerce' ), |
| 211 | 'type' => 'text', |
| 212 | 'desc_tip' => true, |
| 213 | 'description' => $placeholder_text, |
| 214 | 'placeholder' => $this->get_default_subject(), |
| 215 | 'default' => '', |
| 216 | ), |
| 217 | 'heading' => array( |
| 218 | 'title' => __( 'Email heading', 'woocommerce' ), |
| 219 | 'type' => 'text', |
| 220 | 'desc_tip' => true, |
| 221 | 'description' => $placeholder_text, |
| 222 | 'placeholder' => $this->get_default_heading(), |
| 223 | 'default' => '', |
| 224 | ), |
| 225 | 'additional_content' => array( |
| 226 | 'title' => __( 'Additional content', 'woocommerce' ), |
| 227 | 'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text, |
| 228 | 'css' => 'width:400px; height: 75px;', |
| 229 | 'placeholder' => __( 'N/A', 'woocommerce' ), |
| 230 | 'type' => 'textarea', |
| 231 | 'default' => $this->get_default_additional_content(), |
| 232 | 'desc_tip' => true, |
| 233 | ), |
| 234 | 'email_type' => array( |
| 235 | 'title' => __( 'Email type', 'woocommerce' ), |
| 236 | 'type' => 'select', |
| 237 | 'description' => __( 'Choose which format of email to send.', 'woocommerce' ), |
| 238 | 'default' => 'html', |
| 239 | 'class' => 'email_type wc-enhanced-select', |
| 240 | 'options' => $this->get_email_type_options(), |
| 241 | 'desc_tip' => true, |
| 242 | ), |
| 243 | ); |
| 244 | if ( FeaturesUtil::feature_is_enabled( 'email_improvements' ) ) { |
| 245 | $this->form_fields['cc'] = $this->get_cc_field(); |
| 246 | $this->form_fields['bcc'] = $this->get_bcc_field(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Add actions and filters before generating email content. |
| 252 | */ |
| 253 | private function add_pos_customizations() { |
| 254 | // Add action to display unit price in the beginning of the order item meta. |
| 255 | add_action( 'woocommerce_order_item_meta_start', array( $this, 'add_unit_price' ), 10, 4 ); |
| 256 | // Add filter to include additional details in the order item totals table. |
| 257 | add_filter( 'woocommerce_get_order_item_totals', array( $this, 'order_item_totals' ), 10, 3 ); |
| 258 | // Add filter for custom footer text with highest priority to run before the default footer text filtering in `WC_Emails`. |
| 259 | add_filter( 'woocommerce_email_footer_text', array( $this, 'replace_footer_placeholders' ), 1, 2 ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Remove actions and filters after generating email content. |
| 264 | */ |
| 265 | private function remove_pos_customizations() { |
| 266 | // Remove actions and filters after generating content to avoid affecting other emails. |
| 267 | remove_action( 'woocommerce_order_item_meta_start', array( $this, 'add_unit_price' ), 10 ); |
| 268 | remove_filter( 'woocommerce_get_order_item_totals', array( $this, 'order_item_totals' ), 10 ); |
| 269 | remove_filter( 'woocommerce_email_footer_text', array( $this, 'replace_footer_placeholders' ), 1 ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get the email header. |
| 274 | * |
| 275 | * @param mixed $email_heading Heading for the email. |
| 276 | * |
| 277 | * @internal For exclusive usage within this class, backwards compatibility not guaranteed. |
| 278 | */ |
| 279 | public function email_header( $email_heading ) { |
| 280 | wc_get_template( |
| 281 | 'emails/email-header.php', |
| 282 | array( |
| 283 | 'email_heading' => $email_heading, |
| 284 | 'store_name' => $this->get_pos_store_name(), |
| 285 | ) |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get the email footer. |
| 291 | * |
| 292 | * @param mixed $email Email object. |
| 293 | * |
| 294 | * @internal For exclusive usage within this class, backwards compatibility not guaranteed. |
| 295 | */ |
| 296 | public function email_footer( $email ) { |
| 297 | wc_get_template( |
| 298 | 'emails/email-footer.php', |
| 299 | array( |
| 300 | 'email' => $email, |
| 301 | ) |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Add unit price to order item meta start position. |
| 307 | * |
| 308 | * @param int $item_id Order item ID. |
| 309 | * @param array $item Order item data. |
| 310 | * @param WC_Order $order Order object. |
| 311 | */ |
| 312 | public function add_unit_price( $item_id, $item, $order ) { |
| 313 | $unit_price = OrderPriceFormatter::get_formatted_item_subtotal( $order, $item, get_option( 'woocommerce_tax_display_cart' ) ); |
| 314 | echo wp_kses_post( '<br /><small>' . $unit_price . '</small>' ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Add additional details to the order item totals table. |
| 319 | * |
| 320 | * @param array $total_rows Array of total rows. |
| 321 | * @param WC_Order $order Order object. |
| 322 | * @param string $tax_display Tax display. |
| 323 | * @return array Modified array of total rows. |
| 324 | */ |
| 325 | public function order_item_totals( $total_rows, $order, $tax_display ) { |
| 326 | $cash_payment_change_due_amount = $order->get_meta( '_cash_change_amount', true ); |
| 327 | if ( '' !== $cash_payment_change_due_amount ) { |
| 328 | $formatted_cash_payment_change_due_amount = wc_price( $cash_payment_change_due_amount, array( 'currency' => $order->get_currency() ) ); |
| 329 | $total_rows['cash_payment_change_due_amount'] = array( |
| 330 | 'type' => 'cash_payment_change_due_amount', |
| 331 | 'label' => __( 'Change due:', 'woocommerce' ), |
| 332 | 'value' => $formatted_cash_payment_change_due_amount, |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | $auth_code = $order->get_meta( '_charge_id', true ); |
| 337 | if ( ! empty( $auth_code ) ) { |
| 338 | $total_rows['payment_auth_code'] = array( |
| 339 | 'type' => 'payment_auth_code', |
| 340 | 'label' => __( 'Auth code:', 'woocommerce' ), |
| 341 | 'value' => $auth_code, |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | if ( $order->get_date_paid() !== null ) { |
| 346 | $total_rows['date_paid'] = array( |
| 347 | 'type' => 'date_paid', |
| 348 | 'label' => __( 'Time of payment:', 'woocommerce' ), |
| 349 | 'value' => wc_format_datetime( $order->get_date_paid(), get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | return $total_rows; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Enable email template for REST API order valid templates for POS orders. |
| 358 | */ |
| 359 | private function enable_email_template_for_pos_orders() { |
| 360 | add_filter( 'woocommerce_rest_order_actions_email_valid_template_classes', array( $this, 'add_to_valid_template_classes' ), 10, 2 ); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Add this email template to the list of valid templates for POS orders. |
| 365 | * |
| 366 | * @param array $valid_template_classes Array of valid template class names. |
| 367 | * @param WC_Order $order The order. |
| 368 | * @return array Modified array of valid template class names. |
| 369 | */ |
| 370 | public function add_to_valid_template_classes( $valid_template_classes, $order ) { |
| 371 | if ( ! PointOfSaleOrderUtil::is_pos_order( $order ) ) { |
| 372 | return $valid_template_classes; |
| 373 | } |
| 374 | $valid_template_classes[] = get_class( $this ); |
| 375 | return $valid_template_classes; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get the store name from POS settings. |
| 380 | * |
| 381 | * @return string |
| 382 | */ |
| 383 | private function get_pos_store_name() { |
| 384 | $store_name = get_option( 'woocommerce_pos_store_name' ); |
| 385 | return $this->format_string( |
| 386 | empty( $store_name ) ? PointOfSaleDefaultSettings::get_default_store_name() : $store_name |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get the store email from POS settings. |
| 392 | * |
| 393 | * @return string |
| 394 | */ |
| 395 | private function get_pos_store_email() { |
| 396 | return $this->format_string( |
| 397 | get_option( 'woocommerce_pos_store_email', PointOfSaleDefaultSettings::get_default_store_email() ) |
| 398 | ); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get the store phone number from POS settings. |
| 403 | * |
| 404 | * @return string |
| 405 | */ |
| 406 | private function get_pos_store_phone_number() { |
| 407 | return $this->format_string( |
| 408 | get_option( 'woocommerce_pos_store_phone' ) |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get the store address from POS settings. |
| 414 | * |
| 415 | * @return string |
| 416 | */ |
| 417 | private function get_pos_store_address() { |
| 418 | return $this->format_string( |
| 419 | get_option( 'woocommerce_pos_store_address', PointOfSaleDefaultSettings::get_default_store_address() ) |
| 420 | ); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Get the refund and returns policy from POS settings. |
| 425 | * |
| 426 | * @return string |
| 427 | */ |
| 428 | private function get_pos_refund_returns_policy() { |
| 429 | return $this->format_string( |
| 430 | get_option( 'woocommerce_pos_refund_returns_policy' ) |
| 431 | ); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Replace footer text placeholders with POS-specific values. |
| 436 | * |
| 437 | * @param string $footer_text The footer text to be filtered. |
| 438 | * @param mixed $email Email object. |
| 439 | * @return string Modified footer text. |
| 440 | * |
| 441 | * @internal For exclusive usage within this class, backwards compatibility not guaranteed. |
| 442 | */ |
| 443 | public function replace_footer_placeholders( $footer_text, $email ) { |
| 444 | // Only replace placeholders if we're in the context of a POS email. |
| 445 | if ( $email->id !== $this->id ) { |
| 446 | return $footer_text; |
| 447 | } |
| 448 | |
| 449 | return str_replace( |
| 450 | array( |
| 451 | '{site_title}', |
| 452 | '{store_address}', |
| 453 | '{store_email}', |
| 454 | ), |
| 455 | array( |
| 456 | $this->get_pos_store_name(), |
| 457 | $this->get_pos_store_address(), |
| 458 | $this->get_pos_store_email(), |
| 459 | ), |
| 460 | $footer_text |
| 461 | ); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | endif; |
| 466 | |
| 467 | return new WC_Email_Customer_POS_Completed_Order(); |
| 468 |