gpt4 book ai didi

php - 从外部 wordpress 的订单 ID 获取产品 ID 和变体 ID

转载 作者:行者123 更新时间:2023-11-29 02:41:35 25 4
gpt4 key购买 nike

我正在尝试通过 PHP/MYSQL 从 Order ID 查询 WooCommerce Product ID/Variation 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();

关于php - 从外部 wordpress 的订单 ID 获取产品 ID 和变体 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50808658/

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