gpt4 book ai didi

php - 在 Woocommerce 电子邮件通知中根据送货方式显示自定义内容

转载 作者:行者123 更新时间:2023-12-04 14:25:11 25 4
gpt4 key购买 nike

Woocommerce 更新到 3.2 后,下面的代码不再有效。

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

// Only for "Customer Completed Order" email notification
if( 'customer_completed_order' != $email->id ) return;

// Comptibility With WC 3.0+
if ( method_exists( $order, 'get_id' ) ) {
$order_id = $order->get_id();
} else {
$order_id = $order->id;
}
//$order->has_shipping_method('')
$shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
$rate_id = $shipping_method_arr[0][0]; // the rate ID

if ( 'flat_rate:10' == $rate_id ){
echo pll__("Text 1");
} else {
echo pll__("Text 2");
}
}

这段代码有什么错误或过时的地方?
需要进行哪些更改才能使其再次运行?

最佳答案

以下是获取订单中使用的运输方法并使此功能按预期工作的正确方法:

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

// Only for "Customer Completed Order" email notification
if( 'customer_completed_order' != $email->id ) return;

$found = false; // Initializing variable

// Iterating through Order shipping methods
foreach($order->get_shipping_methods() as $value){
$rate_id = $value->get_method_id(); // Get the shipping rate ID
if ( 'flat_rate:10' == $rate_id )
$found = true;
}

if ($found)
echo '<p>'.__("Text 1 (found)","woocommerce").'</p>';
else
echo '<p>'.__("Text 2 (else)","woocommerce").'</p>';
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

测试和工作......

关于php - 在 Woocommerce 电子邮件通知中根据送货方式显示自定义内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270776/

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