id = 'Affinite_alert_customer_email';
$this->customer_email = true;
$this->title = esc_html__('Stock alert info', 'affinite-back-in-stock');
$this->description = esc_html__('Stock alert info', 'affinite-back-in-stock');
$this->heading = esc_html__('Stock alert info', 'affinite-back-in-stock');
$this->subject = esc_html__('Stock alert info from {site_title}', 'affinite-back-in-stock');
// 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_html = 'stock-alert-customer-email.php';
$this->template_plain = 'stock-alert-customer-email-plain.php';
$this->templates = array( 'stock-alert-customer-email.php', 'stock-alert-customer-email-plain.php' );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
}
/**
* Determine if the email should actually be sent and setup email merge variables
*
* @since 0.1
* @param int $order_id
*/
public function trigger( $product_id ) {
$this->product = wc_get_product( $product_id );
if ( ! $this->is_enabled() ) {
return;
}
global $wpdb;
// Custom plugin table - direct query is necessary.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$data = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM ".$wpdb->prefix."stock_alert WHERE productid = %d AND send = '1' ORDER BY ID DESC",
array(
$product_id
)
)
);
if( !empty( $data ) ){
$recipients = array();
foreach( $data as $item ){
$recipients[] = $item->email;
$update = array('send' => 2, 'senddate' => time() );
global $wpdb;
// Custom plugin table - direct query is necessary for data updates.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->update(
$wpdb->prefix.'stock_alert',
$update,
array( 'ID' => $item->ID )
);
}
if( !empty( $recipients ) ){
foreach ($recipients as $recipient) {
// Check if stock_alert_cron table exists before inserting
$table_name = $wpdb->prefix . 'stock_alert_cron';
// Custom plugin table check - direct query is necessary.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) === $table_name;
if ( $table_exists ) {
$data_cron_table = array();
$data_cron_table['email'] = $recipient;
$data_cron_table['subject'] = $this->get_subject();
$data_cron_table['content'] = $this->get_content();
$data_cron_table['headers'] = $this->get_headers();
$data_cron_table['attachments'] = $this->get_attachments();
// Custom plugin table - direct query is necessary for data insertion.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->insert( $table_name, $data_cron_table );
}
$this->send( $recipient, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
return true;
}
}
return false;
}
/**
* get_content_html function.
*
* @since 0.1
* @return string
*/
public function get_content_html() {
return wc_get_template_html( $this->template_html, array(
'product' => $this->product,
'email_heading' => $this->get_heading()
) );
}
/**
* get_content_plain function.
*
* @since 0.1
* @return string
*/
public function get_content_plain() {
return wc_get_template_html( $this->template_plain, array(
'product' => $this->product,
'email_heading' => $this->get_heading()
) );
}
/**
* Initialise settings form fields.
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => esc_html__( 'Enable/Disable', 'affinite-back-in-stock' ),
'type' => 'checkbox',
'label' => esc_html__( 'Enable this email notification', 'affinite-back-in-stock' ),
'default' => 'yes',
),
'subject' => array(
'title' => esc_html__( 'Subject', 'affinite-back-in-stock' ),
'type' => 'text',
'desc_tip' => true,
/* translators: %s: list of placeholders */
'description' => sprintf( esc_html__( 'Available placeholders: %s', 'affinite-back-in-stock' ), '{site_title}, {order_date}, {order_number}' ),
'placeholder' => $this->get_default_subject(),
'default' => '',
),
'heading' => array(
'title' => esc_html__( 'Email heading', 'affinite-back-in-stock' ),
'type' => 'text',
'desc_tip' => true,
/* translators: %s: list of placeholders */
'description' => sprintf( esc_html__( 'Available placeholders: %s', 'affinite-back-in-stock' ), '{site_title}, {order_date}, {order_number}' ),
'placeholder' => $this->get_default_heading(),
'default' => '',
),
'email_type' => array(
'title' => esc_html__( 'Email type', 'affinite-back-in-stock' ),
'type' => 'select',
'description' => esc_html__( 'Choose which format of email to send.', 'affinite-back-in-stock' ),
'default' => 'html',
'class' => 'email_type wc-enhanced-select',
'options' => $this->get_email_type_options(),
'desc_tip' => true,
),
);
}
} // end class