>我正在尝试从订单ID查询WooCommerce产品ID/变体ID
-
如果订单中的产品很简单,请获取产品 ID
-
如果订单中的产品是可变的,则获取变体 ID
-
如果两者(简单和可变(同时获得(产品 ID 和变体 ID(
注意:我正在编码的脚本独立于WordPress。
使用WooCommerce API的各个方面,可以使用以下某些版本来完成此操作。
$order_id = XXX;
$order = wc_get_order( $order_id ); //returns WC_Order if valid order
$items = $order->get_items(); //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)
foreach( $items as $item ) {
//returns the type (i.e. variable or simple)
$type = $item->get_type();
//the product id, always, no matter what type of product
$product_id = $item->get_product_id();
//a default
$variation_id = false;
//check if this is a variation using is_type
if( $item->is_type('variable') ) {
$variation_id = $item->get_variation_id();
}
//more code
}
请注意文档,
wc_get_order();
WC_Order();
WC_Order_Item();
WC_Order_Item_Product();
正如您在注释中提到的"我正在编码的脚本独立于WordPress"。
然后将有一个查询,您可以使用
从产品 ID 获取变体 ID 列表。从wp_posts
中选择 ID,其中post_parent
= "PRODUCT_ID",post_type
像"product_variation">