首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当订单中有备份项时,在WooCommerce电子邮件通知中显示消息

当订单中有备份项时,在WooCommerce电子邮件通知中显示消息
EN

Stack Overflow用户
提问于 2021-12-14 13:31:56
回答 1查看 345关注 0票数 1

我试图在订单确认电子邮件中显示一条特定的消息,如果您的订单中有一种产品处于落后状态。

我正在努力获得正确的功能,以扫描所有的产品,并使我的布尔工作。

我现在的代码是:

代码语言:javascript
复制
add_action( 'woocommerce_email_after_order_table', 'backordered_items_checkout_notice_email', 20, 4 );
function backordered_items_checkout_notice_email( $order, $sent_to_admin, $plain_text, $email ) {
  $found2 = false;
  foreach ( $order->get_items() as $item ) {
            if( $item['data']->is_on_backorder( $item['quantity'] ) ) {
            $found2 = true;
            break;
        }
    }

    if( $found2 ) {
        if ( $email->id == 'customer_processing_order' ) {echo ' <strong>'.__('⌛ One or several products are Currently out of stock. <br/>Please allow 2-3 weeks for delivery.', 'plugin-mve').'</strong><br/>';}
    
    }
}

使用此代码,当您单击“订单”时,页面就会冻结,不会发送电子邮件。但我在后台拿到了订单。

有人能帮我修一下吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-14 13:42:54

您的代码包含一个关键的未理解错误,即:调用null上的成员函数is_on_backorder()

下面的代码将添加customer_processing_order电子邮件通知的消息。另见:How to target other WooCommerce order emails

所以你得到了:

代码语言:javascript
复制
function action_woocommerce_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    // Initialize
    $flag = false;
    
    // Target certain email notification
    if ( $email->id == 'customer_processing_order' ) {
        // Iterating through each item in the order
        foreach ( $order->get_items() as $item ) {
            // Get a an instance of product object related to the order item
            $product = $item->get_product();

            // Check if the product is on backorder
            if ( $product->is_on_backorder() ) {
                $flag = true;
                
                // Stop the loop
                break;
            }
        }
        
        // True
        if ( $flag ) {
            echo '<p style="color: red; font-size: 30px;">' . __( 'My message', 'woocommerce' ) . '</p>';
        }
    }
}
add_action( 'woocommerce_email_after_order_table', 'action_woocommerce_email_after_order_table', 10, 4 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70349709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档