简体   繁体   English

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

[英]Get the Product ID from email templates in WooCommerce 3

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. 在Woocommerce中,我试图找出如何在客户已完成订单电子邮件模板中获取已完成订单的产品ID并将其存储为PHP变量。 This way I will be able to insert it into an external database. 这样,我将能够将其插入到外部数据库中。

I already tried $product->get_id(); 我已经尝试过$product->get_id(); but it does not work. 但它不起作用。

How can get the Product ID in WooCommerce 3+ from email templates? 如何从电子邮件模板获取WooCommerce 3+中的产品ID?

You need to loop through Order items… Normally the WC_Order object is defined through the variable $order mostly defined everywhere in email templates. 您需要遍历Order项目…通常, WC_Order对象是通过变量$order定义的,该变量主要在电子邮件模板的各处定义。

So the code to get the product ID will be: 因此,获取产品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(); 
}

See this related thread: Get Order items and WC_Order_Item_Product in Woocommerce 3 参见以下相关主题: 在Woocommerce 3中获取订单商品和WC_Order_Item_Product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM