| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\Emails; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\RequestAQuote\CPT\QuoteRequestCPT; |
| 4 |
use TierPricingTable\Addons\RequestAQuote\Models\RequestQuoteForm; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class CustomerQuoteEmail extends \WC_Email { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
$this->id = 'tier_pricing_table_quote_request_customer'; |
| 14 |
$this->title = __( 'Quote Request (Customer)', 'tier-pricing-table' ); |
| 15 |
$this->description = __( 'This email is sent to the customer to acknowledge their quote request.', |
| 16 |
'tier-pricing-table' ); |
| 17 |
$this->template_html = 'emails/customer-quote-request.php'; |
| 18 |
$this->template_plain = 'emails/plain/customer-quote-request.php'; |
| 19 |
$this->template_base = dirname( __DIR__ ) . '/views/'; |
| 20 |
|
| 21 |
// Call parent constructor |
| 22 |
parent::__construct(); |
| 23 |
|
| 24 |
$this->customer_email = ''; |
| 25 |
|
| 26 |
add_action( 'tiered_pricing_table/request_quote/quote_request_submitted', array( $this, 'trigger' ), 10, 1 ); |
| 27 |
} |
| 28 |
|
| 29 |
public function trigger( $postId ) { |
| 30 |
$this->object = new \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest( $postId ); |
| 31 |
|
| 32 |
if ( ! $this->object || ! $this->object->getId() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Check form settings |
| 37 |
$formId = $this->object->getMeta('_form_id'); |
| 38 |
$form = RequestQuoteForm::get( $formId ); |
| 39 |
|
| 40 |
if ( $form && ! $form->isCustomerEmailEnabled() ) { |
| 41 |
return; // Customer email is disabled for this form |
| 42 |
} |
| 43 |
|
| 44 |
$this->setup_locale(); |
| 45 |
|
| 46 |
// Determine the customer's email address |
| 47 |
$this->recipient = $this->getCustomerEmail( $postId ); |
| 48 |
|
| 49 |
if ( $this->is_enabled() && $this->get_recipient() ) { |
| 50 |
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), |
| 51 |
$this->get_attachments() ); |
| 52 |
} |
| 53 |
|
| 54 |
$this->restore_locale(); |
| 55 |
} |
| 56 |
|
| 57 |
private function getCustomerEmail( $postId ) { |
| 58 |
$formId = $this->object->getMeta('_form_id'); |
| 59 |
if ( ! $formId ) { |
| 60 |
return $this->get_fallback_email(); |
| 61 |
} |
| 62 |
|
| 63 |
$form = RequestQuoteForm::get( $formId ); |
| 64 |
|
| 65 |
if ( ! $form ) { |
| 66 |
return $this->get_fallback_email(); |
| 67 |
} |
| 68 |
|
| 69 |
// Find the first field with type === 'email' |
| 70 |
$emailFieldName = ''; |
| 71 |
$formFields = $form->getFields(); |
| 72 |
foreach ( $formFields as $field ) { |
| 73 |
if ( isset( $field['type'] ) && $field['type'] === 'email' && ! empty( $field['name'] ) ) { |
| 74 |
$emailFieldName = $field['name']; |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ( $emailFieldName ) { |
| 80 |
$email = $this->object->getMeta( '_' . $emailFieldName ); |
| 81 |
if ( is_email( $email ) ) { |
| 82 |
return $email; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $this->get_fallback_email(); |
| 87 |
} |
| 88 |
|
| 89 |
private function get_fallback_email() { |
| 90 |
if ( is_user_logged_in() ) { |
| 91 |
$user = wp_get_current_user(); |
| 92 |
|
| 93 |
return $user->user_email; |
| 94 |
} |
| 95 |
|
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
public function get_default_subject() { |
| 100 |
return __( '[{site_title}] Thank you for your Quote Request for {product_name}', 'tier-pricing-table' ); |
| 101 |
} |
| 102 |
|
| 103 |
public function get_default_heading() { |
| 104 |
return __( 'Quote Request Received', 'tier-pricing-table' ); |
| 105 |
} |
| 106 |
|
| 107 |
public function get_content_html() { |
| 108 |
return wc_get_template_html( $this->template_html, array( |
| 109 |
'quotePost' => $this->object, |
| 110 |
'emailHeading' => $this->get_heading(), |
| 111 |
'sent_to_admin' => false, |
| 112 |
'plain_text' => false, |
| 113 |
'email' => $this, |
| 114 |
), '', $this->template_base ); |
| 115 |
} |
| 116 |
|
| 117 |
public function get_content_plain() { |
| 118 |
return wc_get_template_html( $this->template_plain, array( |
| 119 |
'quotePost' => $this->object, |
| 120 |
'emailHeading' => $this->get_heading(), |
| 121 |
'sent_to_admin' => false, |
| 122 |
'plain_text' => true, |
| 123 |
'email' => $this, |
| 124 |
), '', $this->template_base ); |
| 125 |
} |
| 126 |
|
| 127 |
public function init_form_fields() { |
| 128 |
$this->form_fields = array( |
| 129 |
'enabled' => array( |
| 130 |
'title' => __( 'Enable/Disable', 'tier-pricing-table' ), |
| 131 |
'type' => 'checkbox', |
| 132 |
'label' => __( 'Enable this email notification', 'tier-pricing-table' ), |
| 133 |
'default' => 'yes', |
| 134 |
), |
| 135 |
'subject' => array( |
| 136 |
'title' => __( 'Subject', 'tier-pricing-table' ), |
| 137 |
'type' => 'text', |
| 138 |
'desc_tip' => true, |
| 139 |
'description' => __( 'This controls the email subject line. Leave blank to use the default subject: ', |
| 140 |
'tier-pricing-table' ) . '<code>' . $this->get_default_subject() . '</code>', |
| 141 |
'placeholder' => $this->get_default_subject(), |
| 142 |
'default' => '', |
| 143 |
), |
| 144 |
'heading' => array( |
| 145 |
'title' => __( 'Email heading', 'tier-pricing-table' ), |
| 146 |
'type' => 'text', |
| 147 |
'desc_tip' => true, |
| 148 |
'description' => __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: ', |
| 149 |
'tier-pricing-table' ) . '<code>' . $this->get_default_heading() . '</code>', |
| 150 |
'placeholder' => $this->get_default_heading(), |
| 151 |
'default' => '', |
| 152 |
), |
| 153 |
'email_type' => array( |
| 154 |
'title' => __( 'Email type', 'tier-pricing-table' ), |
| 155 |
'type' => 'select', |
| 156 |
'description' => __( 'Choose which format of email to send.', 'tier-pricing-table' ), |
| 157 |
'default' => 'html', |
| 158 |
'class' => 'email_type wc-enhanced-select', |
| 159 |
'options' => $this->get_email_type_options(), |
| 160 |
'desc_tip' => true, |
| 161 |
), |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
public function format_string( $string ) { |
| 166 |
$productName = __( 'Sample Product', 'tier-pricing-table' ); |
| 167 |
|
| 168 |
if ( $this->object && $this->object instanceof \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest ) { |
| 169 |
$productId = $this->object->getProductId(); |
| 170 |
$product = wc_get_product( $productId ); |
| 171 |
if ( $product ) { |
| 172 |
$productName = $product->get_name(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
$string = str_replace( '{product_name}', $productName, $string ); |
| 177 |
|
| 178 |
return parent::format_string( $string ); |
| 179 |
} |
| 180 |
|
| 181 |
public function get_submitted_fields() { |
| 182 |
if ( ! $this->object || ! ( $this->object instanceof \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest ) ) { |
| 183 |
return array( |
| 184 |
array( 'label' => __( 'Sample Name', 'tier-pricing-table' ), 'value' => 'John Doe' ), |
| 185 |
array( 'label' => __( 'Sample Email', 'tier-pricing-table' ), 'value' => '[email protected]' ), |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
$customFields = $this->object->getCustomFields(); |
| 190 |
$fields = array(); |
| 191 |
|
| 192 |
foreach ( $customFields as $key => $fieldData ) { |
| 193 |
if ( ! is_array( $fieldData ) ) { |
| 194 |
$fieldData = array( 'label' => ucfirst( str_replace('_', ' ', $key) ), 'value' => $fieldData ); |
| 195 |
} |
| 196 |
$label = isset($fieldData['label']) && !empty($fieldData['label']) ? $fieldData['label'] : ucfirst( str_replace('_', ' ', $key) ); |
| 197 |
$value = isset($fieldData['value']) ? $fieldData['value'] : ''; |
| 198 |
|
| 199 |
$fields[] = array( |
| 200 |
'label' => $label, |
| 201 |
'value' => $value, |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
return $fields; |
| 206 |
} |
| 207 |
} |
| 208 |
|