从 WooCommerce 3 中的电子邮件模板获取产品 ID [英] Get the Product ID from email templates in WooCommerce 3
问题描述
在 Woocommerce 中,我试图找出如何在 Customer 已完成订单电子邮件模板中获取已完成订单的 产品 ID,以将其存储为 PHP 变量.这样我就可以将它插入到外部数据库中.
In Woocommerce I am trying to find out how to get the product id for a completed order inside the Customer completed order email template to store it as a PHP variable. This way I will be able to insert it into an external database.
我已经尝试过 $product->get_id();
但它不起作用.
I already tried $product->get_id();
but it does not work.
如何从电子邮件模板中获取 WooCommerce 3+ 中的产品 ID?
How can get the Product ID in WooCommerce 3+ from email templates?
推荐答案
你需要循环遍历 Order 项... 通常 WC_Order
对象是通过变量 $order
定义的大多在电子邮件模板中随处定义.
You need to loop through Order items… Normally the WC_Order
object is defined through the variable $order
mostly defined everywhere in email templates.
因此获取产品 ID 的代码将是:
So the code to get the product ID will be:
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get the product ID
$product_id = $item->get_product_id();
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get product title (name)
$product_name = $product->get_title(); // or $product->get_name();
// Get product price
$product_price = $product->get_price();
}
请参阅此相关主题:在 Woocommerce 3 中获取订单项和 WC_Order_Item_Product
这篇关于从 WooCommerce 3 中的电子邮件模板获取产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!