gpt4 book ai didi

php - 从 WooCommerce 3 中的电子邮件模板获取产品 ID

转载 作者:行者123 更新时间:2023-12-03 09:02:19 26 4
gpt4 key购买 nike

在 Woocommerce 中,我试图找出如何获取客户已完成订单电子邮件模板中已完成订单的产品 ID,并将其存储为 PHP 变量。这样我就可以将其插入到外部数据库中。

我已经尝试过 $product->get_id(); 但它不起作用。

如何从电子邮件模板获取 WooCommerce 3+ 中的产品 ID?

最佳答案

您需要循环遍历订单项...通常,WC_Order 对象是通过变量 $order 定义的,该变量主要在电子邮件模板中的各处定义。

因此获取产品 ID 的代码将是:

// 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();
}

请参阅此相关主题:Get Order items and WC_Order_Item_Product in Woocommerce 3

关于php - 从 WooCommerce 3 中的电子邮件模板获取产品 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49330876/

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