Php 获取商业订单电子邮件通知中的产品名称或id
我尝试在woocommerce中获取订单的产品名称,现在我可以使用以下方法获取发货方法:Php 获取商业订单电子邮件通知中的产品名称或id,php,woocommerce,product,orders,email-notifications,Php,Woocommerce,Product,Orders,Email Notifications,我尝试在woocommerce中获取订单的产品名称,现在我可以使用以下方法获取发货方法: add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 ); function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) { echo '<p><h4&
add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 );
function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) {
echo '<p><h4>Shipping:</h4> ' . $order->get_shipping_method() . '</p>';
}
但它显示内部服务器错误。请帮忙
如何在订单电子邮件通知中获取产品的名称或ID?由于订单可以包含许多项目,因此您可以在订单中包含许多产品名称。您需要首先获取订单项目,然后循环遍历每个项目以获取产品名称或ID 这可以使用以下方法完成(将替换您的实际代码):
add_action('woocommerce_email_在_order_table之后,'custom_email_在_order_table之后,'10,4');
功能自定义\u电子邮件\u在\u订单\u表之后($order、$sent\u to\u admin、$plain\u text、$email){
//显示所使用的装运方法
回显“”。“'Shipping'、'woocommerce'):”。$order->get\u Shipping\u method()。”;
$product_NAME=array();
//循环Though订单项
foreach($order->get\u items()作为$item\u id=>$item){
$product=$item->get_product();//获取WC_产品对象的实例
$product_id=$item->get_product_id();//获取产品id
//在数组中设置每个产品名称
$product_name[]=$item->get_name();//获取产品名称
}
//显示产品名称
回显“”。“(“产品名称”,“woocommerce”)”:
”。内爆(“,”,$Product\u名称)。“”;
}
代码进入活动子主题(或活动主题)的function.php文件
测试和工作
$order->id()
@dipak_pusti这是错误的…在woocommerce 3+中获取订单ID时使用$Order->get_ID()代码>和对于产品ID$product->get_ID()代码>…正如您所看到的,这个问题询问的是产品ID或产品名称…嗨,Loic,感谢您的回复,我想知道订单中是否只有一个项目,并且该项目的名称已经显示在电子邮件收据上,我只想获取已显示在电子邮件上的名称。我怎么能做到呢?到目前为止,你提供的代码是惊人的!!非常感谢@如果只有一个项目,内爆(“,”,$product\u name)
code仍将输出该唯一名称。即使只有一个项目,您仍然需要使用foreach循环,以使用WC\u Order\u item\u Product
或WC\u Product
方法获取该项目属性。我已经接受了,我可以问其他问题吗?我可以获取产品的ID吗?@JoeyWang您可以在foreach循环之外使用变量$product\u ID
,您将获得最后一个项目产品ID(因此您只有一个项目,这不是问题)。
$order->get_name() or $order->get_id()
add_action( 'woocommerce_email_after_order_table', 'custom_email_after_order_table', 10, 4 );
function custom_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// Displaying the shipping method used
echo '<p><h4>'.__('Shipping', 'woocommerce').':</h4> '.$order->get_shipping_method().'</p>';
$product_names = array();
// Loop thougth order items
foreach( $order->get_items() as $item_id => $item ){
$product = $item->get_product(); // Get an instance of the WC_Product object
$product_id = $item->get_product_id(); // Get the product ID
// Set each product name in an array
$product_names[] = $item->get_name(); // Get the product NAME
}
// Displaying the product names
echo '<p><strong>'.__('Product names', 'woocommerce').':</strong> <br>'.implode( ', ', $product_names ).'</p>';
}