【问题标题】:WooCommerce custom email for custom payment gateway用于自定义支付网关的 WooCommerce 自定义电子邮件
【发布时间】:2015-08-21 18:02:31
【问题描述】:

所以我为 WooCommerce 创建了一个附加插件。我有一个自定义类来扩展“支票”支付网关,以允许用户无需付款即可执行批发结账。到目前为止,此功能完美无缺。问题是我无法让自定义电子邮件与此自定义付款一起使用。我不确定我是否将两个自定义类连接在一起。我是否在触发器中遗漏了某些内容或使用了错误的触发器挂钩?

在这里我包括电子邮件类..

 function add_dealer_invoice_woocommerce_email( $email_classes ) {
// include our custom email class
require_once( 'includes/class-wc-dealer-invoice-email.php' );

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

return $email_classes;
}

add_filter( 'woocommerce_email_classes', 'add_dealer_invoice_woocommerce_email' );
// Register new status for woocomerce 2.2+
function register_dealer_invoice_order_status() {
register_post_status( 'wc-dealer-invoice', array(
    'label'                     => 'Dealer Invoice',
    'public'                    => true,
    'exclude_from_search'       => false,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop( 'Awaiting payment <span class="count">(%s)</span>', 'Awaiting payment <span class="count">(%s)</span>' )
) );
}
 add_action( 'init', 'register_dealer_invoice_order_status' );

这是扩展 WC_Email 的自定义电子邮件类

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

class WC_Dealer_Invoice_Email extends WC_Email {

public function __construct() {

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

    // this is the title in WooCommerce Email settings
    $this->title = 'Dealer Invoice';

    // this is the description in WooCommerce email settings
    $this->description = 'Dealer Invoice Notification emails are sent when a customer places a wholesale order on consignment';

    // these are the default heading and subject lines that can be overridden using the settings
    $this->heading = 'Dealer Invoice Order';
    $this->subject = 'Dealer Invoice 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' ) );
    add_action( 'woocommerce_checkout_order_processed',  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;

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

    // bail if shipping method is not expedited
    if (  'cheque' !=  get_post_meta( $order_id, '_payment_method', true ))
        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 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' ),
                'multipart' => __( 'Multipart', 'woocommerce' ),
            )
        )
    );
}
} // end \WC_Dealer_Invoice_Email class

【问题讨论】:

  • 如果您不接受付款,我不确定您的任何触发操作是否正在触发...woocommerce_order_status_pending_to_processing_notificationwoocommerce_order_status_failed_to_processing_notificationwoocommerce_checkout_order_processed。如果您希望它在您的新自定义状态下触发,您需要过滤 woocommerce_email_actions。您希望它何时发送?用户结账后订单状态如何?
  • 我希望在结帐的最后一步发送电子邮件。最好有一个链接,以便他们以后可以去付款。订单状态显示为经销商发票,这是支票类的扩展。

标签: php email wordpress woocommerce


【解决方案1】:

我认为你只需要在你的function.php中添加一个代码

add_filter('woocommerce_cart_needs_payment', '__return_false');

允许用户在不进行付款的情况下发送订单。您可以从 woocommerce 设置中设置的电子邮件通知。

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 2017-08-05
    • 2014-06-06
    • 2023-03-03
    • 2016-08-20
    • 2020-11-20
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多