php - 当 WooCommerce 中的特定订单项元为空时发送自定义电子邮件

标签 php wordpress woocommerce orders email-notifications

我使用此代码在某些产品中获取元数据。

$meta_data = $item->get_formatted_meta_data($hideprefix = '_', $include_all = false );
foreach ($meta_data as $dato){
$datoo = (array) $dato;
$mess .= $datoo['display_key'].': ' .$datoo['value']. '<br>' ;

}

其中一个键是“Nome File”。

现在,当所有产品中此元为空时,我需要发送邮件。

我需要修改此代码,但我不知道如何...

    // On order completed status
add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 1 );
//add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 1 );
function send_a_custom_email( $order_id ) {
$order = wc_get_order( $order_id ); // The WC_Order object

foreach ( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product(); // The WC_Product object

    // Get the parent product ID for product variations for product categories
    $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if (  has_term( array(115, 117, 555, 556, 789, 786, 554), 'product_cat', $the_id ) ) {
    

有人可以帮助我吗?

最佳答案

以下是当特定订单项元数据丢失(或为空)时发送自定义电子邮件的方式:

add_action('woocommerce_order_status_processing', 'send_a_custom_email', 20, 2 );
// add_action('woocommerce_order_status_on-hold', 'send_a_custom_email', 20, 2 );
function send_a_custom_email( $order_id, $order ) {
    $product_ids = array(); // Initializing
    
    foreach ( $order->get_items() as $item ) {
        $meta_data  = $item->get_meta('Nome File'); // Get order item meta data
        
        if( empty($meta_data) ) {
            $product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
        }
    }
    if ( ! empty($product_ids) ) {
        $recipient = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a747b777f5a7f777b737634797577" rel="noreferrer noopener nofollow">[email protected]</a>'; // Set email recipient
        $subject   = sprintf( __('Order #%d: Has missing item meta data'), $order_id );
        $content   = sprintf( __('On Order #%d, purchased products %s have missing meta data'), $order_id, implode(', ', $product_ids) );
        wp_mail( $recipient, $subject, $content ); // Send email
    }
}

代码位于事件子主题(或事件主题)的functions.php 文件中。它应该可以工作。

关于php - 当 WooCommerce 中的特定订单项元为空时发送自定义电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66873213/

相关文章:

wordpress - Visual Studio代码+ WordPress + Docker:VS代码不提供Apache envvars文件

php - Laravel updateOrCreate 方法——更新你匹配的同一列

php - 使用 PHP 将类添加到 MySQL 数据的 HTML 表的单元格

php - 更改 Woocommerce 中默认选择的支付网关

php - 用户使用 Woocommerce Booking 进行预订时如何发送电子邮件

php - 覆盖子主题中的 WordPress 插件

php - Woocommerce API - 图像问题

php - 如何确保2个远程操作成功完成,并在其中1个远程操作失败时回滚所有操作?

php - curl 问题。 PHP 脚本未随叫随到

php - 在 wordpress 中替换排队脚本的正确方法?