简体   繁体   English

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

[英]How to send a custom email in the admin panel of wordpress

Well I am trying to modify a small plugin. 好吧,我正在尝试修改一个小插件。 This plugin calculate the deposit of an item, after the user paid the deposit then the plugin send to the user an email saying that the deposit has been paid. 该插件计算项目的押金,在用户支付押金后,该插件会向用户发送一封电子邮件,说明押金已经支付。

I want to add a button on the admin panel in the woocommerce order that once is clicked send and email to that user saying how much left to pay. 我想在woocommerce订单的管理面板上添加一个按钮,一旦单击该按钮,就会向该用户发送并发送电子邮件,说明还剩多少钱。

Firstly, I added the button: 首先,我添加了按钮:

//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   
  }

Then I get this by jquery: 然后我通过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');
        }

    });

Obviously this is a test, I want to call in here the function to a custom email: 显然,这是一个测试,我想在此处将功能调用到自定义电子邮件中:

<?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();

Any ideas how can I accomplish this? 有什么想法我该怎么做?

In simple terms: 简单来说:

  1. Register an Ajax endpoint that launches your email function within your plugin 注册一个Ajax端点,该端点将在您的插件中启动您的电子邮件功能
  2. When the button is clicked, fire a request to the Ajax endpoint 单击按钮后,向Ajax端点发出请求
  3. Your email is sent 您的电子邮件已发送
  4. Return what ever you want to the Ajax call and do what ever you want with the returned result. 将所需的内容返回给Ajax调用,并使用返回的结果进行所需的操作。

Registering an Ajax endpoint: 注册一个Ajax端点:

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

Sending Ajax requests with jQuery: 使用jQuery发送Ajax请求:

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

Sending emails from a Wordpress plugin: 从Wordpress插件发送电子邮件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM