如何获取变体父产品 ID。
示例: 我有 ID 为 35 的产品 并且该产品有两种颜色变化 - 红色(ID 351)、黑色(ID 352)
我的代码:
$product = wc_get_product(get_the_ID()); //get_the_ID() is ID 351 and I need this parent ID 35
答案 0 :(得分:1)
使用 wp_get_post_parent_id
,因为变体将其父项作为产品本身。
示例:
$variation_id = get_the_ID();
$product_id = wp_get_post_parent_id($variation_id);