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");
}
}
此代码中有什么问题或过时了?
需要做些哪些改变才能让它再次发挥作用?
答案 0 :(得分:2)
以下是获取订单中使用的Shipping方法的正确方法,并使此功能按预期工作:
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文件中,或者放在任何插件文件中。
经过测试和工作......