当前位置: 首页 > 知识库问答 >
问题:

在WooCommerce电子邮件主题中显示订单项的产品属性值

甄成弘
2023-03-14

在WooCommerce中,我试图获取特定的产品属性值,并将其显示在管理新订单电子邮件通知的主题行中。

我发现了以下代码,但我对如何使其工作知之甚少:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {

    global $woocommerce;
    global $product;       
    {
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
        $subject = sprintf( '[%s] New customer order (# %s) from %s %s',
                               $blogname, $order->id,
                               $order->billing_first_name, $order->billing_last_name );
    } 
    return $subject;
}

我也试过这个(其中xxxxx是我属性的slug):

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {

    global $woocommerce;
    global $product;       
    {  
        $pa_xxxxx_value = get_order_meta($order->id, 'pa_xxxxx', true);
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
        $subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
                              $pa_xxxxx_value, $blogname, $order->id,
                              $order->billing_first_name, $order->billing_last_name );
    } 
    return $subject;
}

但这对他们来说是行不通的。

如何从WooCommerce电子邮件主题中的订单项获取并显示特定的产品属性值?

共有1个答案

邵毅
2023-03-14

订单可以有许多项目,并且自WooCommerce 3以来,您的代码中存在一些错误。

下面的代码将在订单项中搜索特定的产品属性(分类法),如果找到,它将显示具有此产品属性术语名称值的新自定义主题:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
    // HERE define the product attribute taxonomy (start always with "pa_")
    $taxonomy = 'pa_color'; //

    // Loop through order items searching for the product attribute defined taxonomy
    foreach( $order->get_items() as $item ){
        // If product attribute is found
        if( $item->get_meta($taxonomy) ){
            // Custom new subject including the product attribute term name
            $subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
                get_term_by('slug', $item->get_meta($taxonomy), $taxonomy )->name, // Term name
                wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                $order->get_id(),
                $order->get_billing_first_name(),
                $order->get_billing_last_name()
            );
            break; // Stop the loop
        }
    }

    return $subject;
}

代码进入活动子主题(或活动主题)的function.php文件。测试和工作。

 类似资料:
  • 所以我已经为一个在线商店做了一个自定义管理订单模板(重新设计和重新安排了很多)。我已经知道“show image=>false/true”这不是我要找的答案,因为我完全改变了我的电子邮件模板中的一切。现在我的问题是不知道如何展示产品形象。 欢迎任何帮助,因为我是编码的新手。 编辑:这是我现在存在的错误: 下面是我的代码:

  • 我试图在处理订单时Woocommerce自动发送给客户的电子邮件中显示交货日期的值。 我已经创建了一个名为“leveranstid”的高级自定义字段值,我希望它与电子邮件中的每个产品一起显示。我试图将下面的代码添加到functions.php中,但似乎不起作用。什么也看不出来。如果有人能帮我找到问题所在,我将不胜感激?

  • 根据订购的产品,我需要发送两封不同的“新订单”电子邮件。这种情况下,产品存储在不同的位置,我需要发送一封电子邮件,其中只包含存储1中的产品,另一封邮件包含存储2中的产品。如果仅订购了位于存储1的产品,则不应发送电子邮件2,反之亦然。 存储信息是每个产品上的自定义元字段。 我添加了一个自定义电子邮件类,它是的副本,但仅在需要时更改了名称。 我一直在寻找钩子和过滤器,但我不能一路走下去。我也一直在考虑

  • 在Woocommerce上,我已将电子邮件订单详细信息php模板文件中的变量更改为,但我仍然无法在电子邮件通知中显示图像: 我需要添加链接以及产品图像。一旦用户点击图像,它应该重定向到特定的页面。 将消息从假更改为真,但图像仍未显示在网站中。

  • 好的,基本上我们在我们的WooCommerce商店中使用ACF创建了一个自定义字段,以便为特定的产品添加一个“发货延迟”通知。

  • 我无法将产品属性添加到新订单电子邮件中。我已经将下面的代码片段添加到email-order-items.php(在//SKU..part之后),但是什么也没有发生。甚至滴度“位置:”也不可见。有什么想法吗?