如何在wordpress的管理面板中发送自定义电子邮件

时间:2016-05-25 15:45:15

标签: php jquery wordpress email woocommerce

我正在尝试修改一个小插件。此插件计算商品的存款,在用户支付存款后,插件会向用户发送一封电子邮件,说明存款已经支付。

我想在woocommerce订单的管理面板上添加一个按钮,一旦点击发送并通过电子邮件发送给该用户,说明要支付多少钱。

首先,我添加了按钮:

//New action button to send the remaining email
    add_action('woocommerce_admin_order_totals_after_total', array($this, 'pay_remaining_email'));

  public function pay_remaining_email(){
    ?>  
    <tr>    
        <button type="button" class="button pay_remaining button-primary" title="Send">
            <span>Send remaining email</span>
        </button>
    </tr>
    <?php   
  }

然后我通过jquery得到这个:

  /* This is will get the button from the admin panel to send the remianing email */
   $('.pay_remaining').click(function(){
        var test = 2;
        if (test =! 0){
            alert('It works');
        } else {
            alert('It isnt work');
        }

    });

显然这是一个测试,我想在这里调用自定义电子邮件的功能:

<?php

if ( ! defined( 'ABSPATH' ) ) {
  exit; // Exit if accessed directly
}

if ( ! class_exists( 'WC_Deposits_Email_Customer_Partially_Paid_Complete' ) ) :

/**
 * Customer Partially Paid Email
 *
 * An email sent to the customer when an order is not been paid completed seven days before.
 *
 */
class WC_Deposits_Email_Customer_Partially_Paid_Complete extends WC_Email {

  /**
   * Constructor
   */
  function __construct() {

    $this->id         = 'customer_partially_paid';
    $this->title      = __( 'Partial Payment Received', 'woocommerce-deposits' );
    $this->description    = __( 'This is an order notification sent to the customer after payment the deposit, containing order details and a link to pay the remaining balance.', 'woocommerce-deposits' );

    $this->heading      = __( 'Thank you for your order', 'woocommerce-deposits' );
    $this->subject        = __( 'Your {site_title} order receipt from {order_date}', 'woocommerce-deposits' );

    $this->template_html  = 'emails/customer-order-partially-paid.php';
    $this->template_plain   = 'emails/plain/customer-order-partially-paid.php';

    // Triggers for this email
    add_action( 'pay_remaining_email', array( $this, 'trigger' ) );


    // Call parent constructor
    parent::__construct();

    $this->template_base = WC_DEPOSITS_TEMPLATE_PATH;
  }

  /**
   * trigger function.
   *
   * @access public
   * @return void
   */
  function trigger( $order_id ) {

    if ( $order_id ) {
      $this->object     = wc_get_order( $order_id );
      $this->recipient  = $this->object->billing_email;

      $this->find['order-date']      = '{order_date}';
      $this->find['order-number']    = '{order_number}';

      $this->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
      $this->replace['order-number'] = $this->object->get_order_number();
    }

    if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
      return;
    }

    $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
  }

  /**
   * get_content_html function.
   *
   * @access public
   * @return string
   */
  function get_content_html() {
    ob_start();
    wc_get_template( $this->template_html, array(
      'order'     => $this->object,
      'email_heading' => $this->get_heading(),
      'sent_to_admin' => false,
      'plain_text'    => false
    ), '', $this->template_base );
    return ob_get_clean();
  }

  /**
   * get_content_plain function.
   *
   * @access public
   * @return string
   */
  function get_content_plain() {
    ob_start();
    wc_get_template( $this->template_plain, array(
      'order'         => $this->object,
      'email_heading' => $this->get_heading(),
      'sent_to_admin' => false,
      'plain_text'    => true
    ), '', $this->template_base );
    return ob_get_clean();
  }
}

endif;

return new WC_Deposits_Email_Customer_Partially_Paid_Complete();

任何想法我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

简单来说:

  1. 注册一个在插件中启动电子邮件功能的Ajax端点
  2. 单击该按钮时,向Ajax端点发出请求
  3. 您的电子邮件已发送
  4. 返回您想要的Ajax调用,并使用返回的结果执行您想要的任何操作。
  5. 注册Ajax端点:

      

    https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

    使用jQuery发送Ajax请求:

      

    http://api.jquery.com/jquery.ajax/

    从Wordpress插件发送电子邮件:

      

    https://developer.wordpress.org/reference/functions/wp_mail/