首页 > 解决方案 > 在 WooCommerce 电子邮件通知中显示特定产品属性

问题描述

我无法将产品属性添加到 WooCommerce 新订单电子邮件。我已将下面的代码段添加到 email-order-items.php(在 // SKU.. 部分之后),但没有任何反应。甚至标题“位置:”也不可见。对此有什么想法吗?

// Attribute
if ( $item_meta->meta ) {echo '<br/><small>Location: ' . nl2br( $product->get_attribute( 'location' ) ) . '</small>';}

标签: phpwordpresswoocommercecustom-taxonomyemail-notifications

解决方案


更新- 不要覆盖 Woocommerce 模板,而是首先尝试使用可用的钩子,例如:

add_action( 'woocommerce_order_item_meta_start', 'add_download_links_to_thank_you_page', 10, 3 );
function add_download_links_to_thank_you_page( $item_id, $item, $order ) {
    // Set below your product attribute taxonomy (always starts with "pa_")
    $taxonomy = 'pa_location';

    // On email notifications
    if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
        $product    = $item->get_product();
        $label_name = get_taxonomy( $taxonomy )->labels->singular_name;

        if ( $term_names = $product->get_attribute( $taxonomy ) ) {
            echo '<br/><small>' . $label_name . ': ' . nl2br( $term_names ) . '</small>';
        }
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。

在此处输入图像描述

您也可以改用woocommerce_order_item_meta_end钩子。


推荐阅读