gpt4 book ai didi

php - 基于自定义字段值的不同 WooCommerce 电子邮件 header

转载 作者:行者123 更新时间:2023-12-04 15:00:54 24 4
gpt4 key购买 nike

我正在尝试根据订单自定义字段“送货类型”更改我的电子邮件 header ,以帮助商店员工确定订单是送货还是取货,他们希望电子邮件 header 采用颜色编码。

这里有几篇有用的文章解释了如何解除绑定(bind) WooCommerce 电子邮件 header ,然后有效地在每个电子邮件模板(新订单、处理等)中手动添加对 email-header.php 模板的调用或使用 switch 语句根据电子邮件类型应用新 header 。

我正在尝试根据一些自定义订单元数据自定义 email-header.php 模板,用于新订单电子邮件通知。

目前我在 admin-new-order.php 模板中执行此操作,但是因为您必须全局取消设置/取消绑定(bind) header ,所以您必须将调用添加到email-header.php 手动为每个邮件类型/模板创建模板。

基于 Woocommerce different headers for each email types答案代码,这是我的代码尝试:

add_action( 'init', 'replace_email_header_hook' );

function replace_email_header_hook(){
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'woocommerce_email_header', 'woocommerce_email_header', 10, 2 );
}

function woocommerce_email_header( $email_heading, $email ) {

$order = $email->object;

$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

$del_type = get_post_meta( $order_id, 'delivery_type', true );

switch($email->id) {
case 'new_order':
if ($del_type == 'delivery') {

$template = 'emails/email-header-alt.php';

}

else if ($del_type == 'pickup') {


$template = 'emails/email-header.php';


}
break;
default:
$template = 'emails/email-header.php';
}
wc_get_template( $template, array( 'email_heading' => $email_heading ) );
}

这个问题似乎与 $order_id 变量有关,当试图从这个 Hook 中的 Order 对象中获取它时,我不确定这是否可能。

最佳答案

您代码中的主要错误是 else if,它应该是 elseif,您应该以不同的方式重命名您的自定义函数 woocommerce_email_header

$email->object 没有问题,它是 WC_Order 对象。您可以使用 $email->object->get_id() 5if needed) 获取订单 ID。

自 WooCommerce 3 以来,您的代码也可以得到简化和优化。请尝试以下操作:

add_action( 'init', 'customizing_woocommerce_email_header' );
function customizing_woocommerce_email_header(){
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'woocommerce_email_header', 'custom_email_header', 10, 2 );
}

function custom_email_header( $email_heading, $email ) {
$template = 'email-header.php'; // Default template

if ( 'new_order' === $email->id && 'delivery' === $email->object->get_meta( 'delivery_type' ) ) {
$template = 'email-header-alt.php'; // Custom template
}
wc_get_template( 'emails/'.$template, array( 'email_heading' => $email_heading ) );
}

代码进入事件子主题(或事件主题)的 functions.php 文件。经过测试并有效。

关于php - 基于自定义字段值的不同 WooCommerce 电子邮件 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66969703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com