<?php
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
/**
* A custom Expedited Order WooCommerce Email class
*
* @since 0.1
* @extends \WC_Email
*/
class WC_AMO_New_Order_Email extends WC_Email
{
/**
* Ключ идентификатор типа заказа
*/
public $type_auth_key;
/**
* Set email defaults
*
* @since 0.1
*/
public function __construct($plugin_dir)
{
// set ID, this simply needs to be a unique name.
$this->id = 'wc_amo_new_order';
// this is the title in WooCommerce Email settings.
$this->title = 'Новые заказы для АМО';
// Заполняем ключ идентификатор типа заказа (значение по умолчанию).
$this->type_auth_key = 'hummelua_cartorder';
// this is the description in WooCommerce email settings.
$this->description = 'Письмо - уведомление о новом заказе будет отправлено в специальном формате для интеграции с AmoCRM';
// these are the default heading and subject lines that can be overridden using the settings.
$this->heading = __('New customer order', 'woocommerce');
$this->subject = __('[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce');
// these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar.
$this->template_base = $plugin_dir . '/templates/';
$this->template_html = 'admin-new-order-html.php';
$this->template_plain = 'admin-new-order-plain.php';
// Trigger on new paid orders.
add_action('woocommerce_checkout_order_processed_notification', array($this, 'trigger'));
add_action('hml_quick_order_processed_notification', array($this, 'trigger'));
// Call parent constructor to load any other defaults not explicity defined here.
parent::__construct();
// this sets the recipient to the settings defined below in init_form_fields().
$this->recipient = $this->get_option('recipient');
// if none was entered, just use the WP admin email as a fallback.
if (!$this->recipient) {
$this->recipient = get_option('admin_email');
}
}
/**
* Determine if the email should actually be sent and setup email merge variables
*
* @since 0.1
*
* @param int $order_id ID заказа
*/
public function trigger($order_id)
{
// bail if no order ID is present.
if (!$order_id) {
return;
}
// setup order object.
$this->object = new WC_Order($order_id);
/**
* Здесь мы можем проверить некоторые условия которые отпределяют
* необходимость отправки этого письма
* if ( условие ) { return; }
*/
$tak = get_post_meta($order_id, 'type_auth_key', true);
if ('hummelua_quickorder' === $tak) {
$this->type_auth_key = $tak;
}
// replace variables in the subject/headings.
$this->find[] = '{order_date}';
$this->replace[] = date_i18n(wc_date_format(), $this->object->get_date_created()->getOffsetTimestamp());
$this->find[] = '{order_number}';
$this->replace[] = $this->object->get_order_number();
if (!$this->is_enabled() || !$this->get_recipient()) {
return;
}
// woohoo, send the email!
$success = $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
if ($success) {
// добавить примечание к заказу что отправлено письмо для амо.
$this->object->add_order_note(
sprintf(
'Уведомление для AmoCRM отправлено на %s.',
$this->recipient
)
);
} else {
$msg = 'Произошла ошибка отправки письма в AmoCRM по заказу #' . $order_id . '. Истрочник WC_AMO_New_Order_Email.';
$this->send('tpychev@gmail.com', '[Hml] - Error to send mail!', $msg, $this->get_headers(), '');
}
}
/**
* Get email subject.
*
* @return string
*/
public function get_subject()
{
switch ($this->type_auth_key) {
case 'hummelua_quickorder':
$this->subject = '[Hml-{order_number}] Быстрый заказ — {order_date}';
break;
case 'hummelua_cartorder':
$this->subject = '[Hml-{order_number}] Заказ из корзины — {order_date}';
break;
default:
$this->subject = '[Hml-{order_number}] Новый заказ — {order_date}';
}
return apply_filters('woocommerce_email_subject_' . $this->id, $this->format_string($this->subject), $this->object);
}
/**
* get_content_html function.
*
* @since 0.1
* @return string
*/
public function get_content_html()
{
return wc_get_template_html(
$this->template_html,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => true,
'plain_text' => false,
'email' => $this
),
$this->template_base,
$this->template_base
);
}
/**
* get_content_plain function.
*
* @since 0.1
* @return string
*/
public function get_content_plain()
{
return wc_get_template_html(
$this->template_plain,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => true,
'plain_text' => true,
'email' => $this
),
$this->template_base,
$this->template_base
);
}
/**
* Initialize Settings Form Fields
*
* @since 2.0
*/
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'type' => 'checkbox',
'label' => 'Enable this email notification',
'default' => 'yes'
),
'recipient' => array(
'title' => 'Recipient(s)',
'type' => 'text',
'description' => sprintf('Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr(get_option('admin_email'))),
'placeholder' => '',
'default' => ''
),
'subject' => array(
'title' => 'Subject',
'type' => 'text',
'description' => sprintf('This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject),
'placeholder' => '',
'default' => ''
),
'heading' => array(
'title' => 'Email Heading',
'type' => 'text',
'description' => sprintf(__('This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.'), $this->heading),
'placeholder' => '',
'default' => ''
),
'email_type' => array(
'title' => 'Email type',
'type' => 'select',
'description' => 'Choose which format of email to send.',
'default' => 'html',
'class' => 'email_type',
'options' => array(
'plain' => __('Plain text', 'woocommerce'),
'html' => __('HTML', 'woocommerce'),
)
)
);
}
}