【问题标题】:Woocommerce Refund EmailWoocommerce 退款电子邮件
【发布时间】:2014-10-22 01:51:34
【问题描述】:

我进行了大量搜索,尽管我发现有用户询问如何实现以下目标,但据我所知,没有任何可行的解决方案示例。

问题是关于非常流行的 Wordpress 插件“Woocommerce”。该插件带有一个电子邮件系统,使电子商务网站所有者和客户的生活更轻松。一个问题是,当商店经理将订单状态更改为“已退款”时,没有发送电子邮件。有人说这是因为这是一个手动过程。确实,这是店主通过商家帐户或贝宝帐户进行的过程。但是一旦完成,店主就会登录到他们的 wordpress 管理面板并将订单状态更改为已退款,这将有助于生成电子邮件并将其发送给客户。

这是我看到的要求。

所以我决定在 http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/#comment-553147

我正在尝试在订单状态更新为“已退款”时发送电子邮件。

这是初始插件文件的代码

<?php
/**
 * Plugin Name: WooCommerce Custom Expedited Order Email
 * Plugin URI: http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/
 * Description: Demo plugin for adding a custom WooCommerce email that sends admins an email when an order is received with expedited shipping
 * Author: SkyVerge
 * Author URI: http://www.skyverge.com
 * Version: 0.1
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 */

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

/**
 *  Add a custom email to the list of emails WooCommerce should load
 *
 * @since 0.1
 * @param array $email_classes available email classes
 * @return array filtered available email classes
 */
function add_expedited_order_woocommerce_email( $email_classes ) {

    // include our custom email class
    require( 'includes/class-wc-expedited-order-email.php' );

    // add the email class to the list of email classes that WooCommerce loads
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();

    return $email_classes;

}
add_filter( 'woocommerce_email_classes', 'add_expedited_order_woocommerce_email' );

这是我班级代码的链接

<?php

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

/**
 * A custom Expedited Order WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
class WC_Expedited_Order_Email extends WC_Email {

    /**
 * Set email defaults
 *
 * @since 0.1
 */
    public function __construct() {

        // set ID, this simply needs to be a unique name
        $this->id = 'wc_expedited_order';

        // this is the title in WooCommerce Email settings
        $this->title = 'Refunded Order Email';

        // this is the description in WooCommerce email settings
        $this->description = 'Refunded Emails are sent when an order status has been changed to Refunded';

        // these are the default heading and subject lines that can be overridden using the settings
        $this->heading = 'Refunded Order';
        $this->subject = 'Refunded Order';

        // 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  = 'emails/admin-new-order.php';
        $this->template_plain = 'emails/plain/admin-new-order.php';

        // Trigger on new paid orders
        add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_failed_to_processing_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
     */
    public function trigger( $order_id ) {

        // bail if no order ID is present
        if ( ! $order_id )
            return;

        $order = new WC_Order( $order_id );

          //bail if not a refunded order
        if ( 'refunded' !== $order->status ) {
              return;
        }

        // setup order object
        $this->object = new WC_Order( $order_id );

        // bail if shipping method is not expedited
        //if ( ! in_array( $this->object->get_shipping_method(), array( 'Three Day Shipping', 'Next Day Shipping' ) ) )
            //return;

        // replace variables in the subject/headings
        $this->find[] = '{order_date}';
        $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );

        $this->find[] = '{order_number}';
        $this->replace[] = $this->object->get_order_number();

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

        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }

    /**
     * get_content_html function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_html() {
        ob_start();
        woocommerce_get_template( $this->template_html, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }


    /**
     * get_content_plain function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_plain() {
        ob_start();
        woocommerce_get_template( $this->template_plain, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }

    /**
     * Initialize Settings Form Fields
     *
     * @since 0.1
     */
    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',
                    'html'      => 'HTML', 'woocommerce',
                    'multipart' => 'Multipart', 'woocommerce',
                )
            )
        );
    }


} // end \WC_Expedited_Order_Email class

这是我的插件中仅有的 2 个文件。我已激活它,它在 woo commerce 电子邮件选项卡的电子邮件列表中显示为电子邮件。很遗憾,更新订单状态时不会发送电子邮件。

谁能告诉我为什么这不起作用?

我收到了一个人的一些反馈,他说了以下内容

“您添加触发器的操作用于处理订单状态更改的待处理/失败 - http://cld.wthms.co/cZzw 您希望这些是与退款订单相关的操作,例如: add_action( 'woocommerce_order_status_refunded', array( $this, 'trigger' ) ); (对于 woocommerce 的电子邮件类的确切了解)"

我正在使用 Woocommerce 2.1.12

【问题讨论】:

  • @zen 建议的解决方案:add_action( 'woocommerce_order_status_refunded', array( $this, 'trigger' ) ); __construct() 方法对您不起作用吗?
  • @birgire 是的,刚刚尝试过。不行。
  • 如果你想获得额外的功能,你可以花 18 美元解决这个问题。我和这家公司没有任何关系。 codecanyon.net/item/woocommerce-refunds-system/8746341
  • @Len_D,我不介意支付 18 美元,但这带有我不想要的整个争议系统。
  • 最坏的情况是你可以看到他如何执行通知并编写你自己的函数。如果你付钱给他,就不会犯规。

标签: php wordpress woocommerce


【解决方案1】:

主要问题是woocommerce_order_status_refunded钩子默认没有注册send_transactional_email回调,所以当订单状态变为已退款。

您可以通过以下方式进行更改:

/**
 * Register the "woocommerce_order_status_refunded" hook which is necessary to
 * allow automatic email notifications when the order is changed to refunded.
 * 
 * @see http://stackoverflow.com/a/26413223/2078474
 */
add_action( 'woocommerce_init', function() {
    add_action( 
        'woocommerce_order_status_refunded', 
        array( WC(), 'send_transactional_email' ),
        10, 
        10 
    );
});

同时确保您已在 Woo 设置 -> 电子邮件 选项卡的相应部分启用它:

默认情况下,会为自动电子邮件通知注册以下操作:

woocommerce_low_stock
woocommerce_no_stock
woocommerce_product_on_backorder
woocommerce_order_status_pending_to_processing
woocommerce_order_status_pending_to_completed
woocommerce_order_status_pending_to_on-hold
woocommerce_order_status_failed_to_processing
woocommerce_order_status_failed_to_completed
woocommerce_order_status_completed
woocommerce_new_customer_note
woocommerce_created_customer

更新:

好消息,@helgatheviking 刚刚合并了她的 WooCommerce 拉取请求(请参阅下面的 cmets)。

这意味着我们应该能够使用新的woocommerce_email_actions 过滤器:

add_filter( 'woocommerce_email_actions', function( $email_actions ) {
    $email_actions[] = 'woocommerce_order_status_refunded';
    return $email_actions;
});

在 WooCommerce 2.3+ 中。

类似的做法也适用于其他非默认电子邮件操作,例如 woocommerce_order_status_cancelled

【讨论】:

  • 不错的收获。但是你不能简单地做add_action( 'woocommerce_order_status_refunded', array( WC(), 'send_transactional_email' ), 10, 10 );吗? @zen 这个答案结合@len_D 更新触发器的建议应该可以做到。
  • @helgatheviking 谢谢,这是一个有趣的难题要解决;-) 经过一些试验和错误后,这种解决方法似乎适用于我的安装。我将其包装到 woocommerce_init 操作回调中的原因是为了避免 WC() 成为未定义的函数,如果您的插件在 WooCommerce 插件之前运行并且包含您建议的裸操作调用,则可能会发生这种情况。
  • 我已经开始命名函数so_25544762() 或者它们在stackoverflow 中的ID。这确保它们是独一无二的,而不需要任何创造力。可能应该在核心中有一个过滤器来添加/删除电子邮件触发器。如果有人向 Woo 发送了拉取请求,他们可能会添加它。我有点惊讶退款电子邮件还没有在核心中。
  • 已被合并!所以它应该在 WC 2.3 中可用。
  • 没关系,我想通了。此操作add_action( 'woocommerce_init', function() { add_action( 'woocommerce_order_status_refunded', array( WC(), 'send_transactional_email' ), 10, 10 ); }); 需要在主插件文件中而不是电子邮件类中。
【解决方案2】:

我认为问题是你在打电话

add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );

尝试调用:

add_action( 'woocommerce_order_status_refunded', array( $this, 'trigger' ) );

【讨论】:

  • 我试过了,但还是不行。没有发送电子邮件。
  • @zen 我认为如果你将 birgire 的答案与这个答案结合起来,你应该能够让它工作,但你需要它们两者。
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 2021-12-27
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多