从wordpress外部的订单id获取产品id和变体id

pcww981p  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(420)

我正在尝试从订单id中php/mysql查询商品id/变体id
如果订单中的产品很简单,请获取产品id
如果订单中的产品是可变的,则获取变体id
如果两者(简单和变量)都得到(产品id和变量id)
注意:我编写的脚本独立于wordpress。

q5lcpyga

q5lcpyga1#

正如你在注解中提到的“我编写的脚本独立于wordpress”。
然后会有一个您可以使用的查询
从产品id获取变体id列表。
从中选择id wp_posts 哪里 post_parent =“产品标识”和 post_type 比如“产品变化”

3duebb1j

3duebb1j2#

通过使用WooCommerceAPI的各个方面,可以使用以下的一些版本来实现这一点。

$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();

相关问题