我知道这可能非常简单,但我似乎无法得到它。我为Woocommerce开发了一个插件,只有在购买数字下载时才会向客户发送额外的电子邮件。
我一切正常,它目前完美地发送电子邮件。现在我只想在下载购买时发送它,所以我知道我应该使用它:if ($order->has_downloadable_item() ){
但是,如果我在此if语句中包装class WC_Gift_Order_Email extends WC_Email
,我会收到以下错误:
Fatal error: Call to a member function has_downloadable_item() on a non-object in .../wp-content/plugins/woocommerce-gift-receipt-emails/includes/class-wc-gift-receipt-order-email.php on line 12
那么在哪里添加这个if语句以确保它只在购买数字下载时发送的最佳位置是什么?我有正确的代码,只是不知道放在哪里或最好的方法来实现它。
这是我的woocommerce-gift-receipt-order-email.php文件:
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_gift_receipt_order_woocommerce_email( $email_classes ) {
// include our custom email class
require( 'includes/class-wc-gift-receipt-order-email.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['WC_Gift_Order_Email'] = new WC_Gift_Order_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_gift_receipt_order_woocommerce_email' );
我应该在某处添加它还是应该将它添加到我的class-ac-gift-receipt-order-email.php文件中?
答案 0 :(得分:0)
我需要将if语句添加到我的WC_Email类并将其用作对象。
正确(工作)方法:
function trigger( $order_id ) {
global $woocommerce;
if ( $order_id ) {
$this->object = new WC_Order( $order_id );
if ($this->object->has_downloadable_item()) {
$this->recipient = $this->object->billing_email;
$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;
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}