我刚刚安装了这个插件WooCommerce UPC, EAN, and ISBN并激活它,以便在我的单一产品页面上有GTIN字段.

一切正常,我有显示的值在里面的字段(例如:产品代码:123456,显示在产品页面上).

我想要的是当有人下订单时,当我在Outlook上收到订单确认邮箱时,会有一个邮箱模板显示产品标题、价格、数量、BUT not showing the GTIN product code.

如何提取/显示GTIN to WooCommerce邮箱模板.

推荐答案

以下是从产品中获取GTIN代码的方法,并在提交订单时将其保存为自定义订单项目,以便在订单和邮箱通知中显示:

// Utility function to get the GTIN code from the product object
function get_product_gtin_code( $product ) {
    return $product->get_meta('hwp_product_gtin');
}

// Utility function to get the GTIN label name
function get_gtin_label() {
    $label = get_option('hwp_gtin_text');
    return esc_html__( $label ? $label : 'GTIN');
}

// Save Product GTIN as custom order item and display it on orders (items) and emails
add_action( 'woocommerce_checkout_create_order_line_item', 'save_and_display_gtin_everywhere', 10, 4 );
function save_and_display_gtin_everywhere( $item, $item_key, $values, $order ) {
    if ( $gtin_code = get_product_gtin_code($values['data']) ) {
        $item->add_meta_data( 'gtin_code', $gtin_code );
    }
}

// Add readable "meta key" label name replacement
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 10, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
    if( $item->get_type() === 'line_item' && $meta->key === 'gtin_code' ) {
        $display_key = get_gtin_label();
    }
    return $display_key;
}

// Utility function to get the GTIN code from the order item object
function get_order_item_gtin_code( $item ) {
    return $item->get_meta('gtin_code');
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.

Php相关问答推荐

提交表格后的重定向不起作用

try 修改这个PHP代码如果语句在WordPress""

Symfony Monolog:使用多个格式化程序

Postgrid Webhook不叫laravel

PHP邮件表单无法识别重音/特殊字符

Msgraph-sdk-php v2如何从返回对象中获取数据?

在WooCommerce我的帐户部分添加带有自定义链接的自定义菜单项

如何将WooCommerce产品属性术语名称显示为链接术语?

多行PHP属性声明中是否允许单行注释

使用php创建表格和插入数据

如何在UML类图中可视化变量引用数组

从两个日期创建日期范围,然后为范围中的每个日期复制子数组

在Laravel中创建辅助函数时的约定

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

htaccess 重定向子域错误的 url

为什么foreach循环会输出每个结果?

服务器遇到内部错误或配置错误,无法完成您的请求

Laravel 8:会话不适用

使用来自 PHP 表单的数据更新 xml 命名空间

PHP 自动函数参数和变量