php - 从外部 wordpress 的订单 ID 中获取产品 ID 和变体 ID
问题描述
我正在尝试 PHP/MYSQL从订单 ID查询 WooCommerce产品 ID/变体ID
如果订单中的产品很简单,请获取产品 ID
如果订单中的产品是可变的,请获取 Variation ID(s)
如果两者(简单和可变)都获得(产品 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
}
注意文档,
推荐阅读
- database-normalization - 化为第一范式
- neo4j - 如何使用 Kafka 和 neo4j-streams 配置 Neo4j 接收器实例
- python - 将多个键/值关联到 defaultdict(list)
- spring - 使用 Oauth2 的 SPA 和简单后端 - 使用 http 会话的方式
- json - 需要帮助添加到 python3 中的 JSON 字典
- excel - Excel VBA - 组合多个工作表选择更改
- javascript - 在 javascript 中使用 OOP 启动和停止计时器
- nginx - 如何使用 NGINX 反向代理,并将端口和路径代理到上游 url?
- python - 这是在网络抓取中旋转代理的正确方法吗
- python - 如何根据孔的数量计算对象