【发布时间】:2017-11-19 00:41:50
【问题描述】:
不久前,我发布了一个关于问题的问题,我需要根据订购的产品所属的存储(自定义字段)发送不同的电子邮件。 因此,如果只订购了属于 Storage1 的产品,则应该只发送 Email1。如果订购了属于 Storage1 的一种产品和属于 Storage2 的一种产品,则发送的 email1 将包含 product1,而 email2 将包含 product2。 原问题链接:Custom order emails depending on product meta data
现在一切正常,但由于某种原因,我收到了两封电子邮件。如果我从 Storage1 订购一件产品,我会收到两封电子邮件 1 发送到我的收件箱...无论我订购 5、10 还是 20 件产品,我只会收到一封额外的电子邮件,但这已经足够了。
所以我想看看是否有人遇到过同样的事情,以及他们是如何解决的?
为了清楚起见,我将在此处再次添加代码。
我复制了class-wc-new-order 并创建了两个与原始类完全相同的新类。我将 ID 和类名分别更改为 storage 1 和 storage 2。
我通过执行以下操作来加载类:
//Add our custom class to WC email classes
add_filter( 'woocommerce_email_classes', [ $this, 'custom_order_email_add_email_classes' ], 10, 1 );
function custom_order_email_add_email_classes( $email_classes ) {
require( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage1.php' );
require( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage2.php' );
$email_classes['WC_Email_New_Order_Storage1'] = new WC_Email_New_Order_Storage1();
$email_classes['WC_Email_New_Order_Storage2'] = new WC_Email_New_Order_Storage2();
return $email_classes;
}
然后我在两个电子邮件类中编辑了the trigger()-function。
请注意,除了测试之外,我没有对触发操作进行任何更改,但我仍然每次都会收到双封电子邮件,无论触发操作处于活动状态。如果我停用所有触发操作,我当然不会收到任何电子邮件。
public function trigger( $order_id, $order = false ) {
$trigger = false;
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product = $item->get_product();
if ( $product->get_meta( '_product_storage' ) == 'storage2' ) {//storage1 in the other email class
$trigger = true;
}
}
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
if( $trigger === true) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
else {
return;
}
}
为了从 Email1 中过滤掉 Storage2 产品,我必须覆盖 email-order-details.php。此代码在模板中的<tbody> 下方输入,我从order-email-items.php 中获取代码,默认用于显示产品。
编辑 email-order-details.php:
if ( $email->id == 'new_order_storage1' ) {
$items = $order->get_items();
foreach( $items as $item_id => $item ) {
$product = $item->get_product();
if ( $product->get_meta( '_product_storage' ) == 'storage1' ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $product->get_image_id() ? current( wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' ) ) : wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Product image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;" /></div>', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false );
// SKU
if ( $show_sku && is_object( $product ) && $product->get_sku() ) {
echo ' (#' . $product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta( $item );
if ( $show_download_links ) {
wc_display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ); ?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php
}
}
}
else if ( $email->id == 'new_order_storage2' ) {
$items = $order->get_items();
foreach( $items as $item_id => $item ) {
$product = $item->get_product();
if ( $product->get_meta( '_product_storage' ) == 'storage2' ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $product->get_image_id() ? current( wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' ) ) : wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Product image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;" /></div>', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false );
// SKU
if ( $show_sku && is_object( $product ) && $product->get_sku() ) {
echo ' (#' . $product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta( $item );
if ( $show_download_links ) {
wc_display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ); ?></td>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>
<?php
}
}
}
else {
echo wc_get_email_order_items( $order, array(
'show_sku' => $sent_to_admin,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => $plain_text,
'sent_to_admin' => $sent_to_admin,
) );
}
如果你们中的任何人有任何答案或建议,我将非常感激,如果有更好的方法可以做到这一点,我会全力以赴。
问候,
【问题讨论】:
标签: wordpress email woocommerce