从Woocommerce 3中的可变产品获取版本ID

Ale*_*Pvn 2 php wordpress children product woocommerce

必须使用键[0]从数组中获取值,但是数组在对象中。如何将其放入变量中?

  WC_Product_Variable Object ( [children:protected] => Array ( [0] => 344 [1] => 345 ) [visible_children:protected] => Array ( [0] => 344 [1] => 345 ) 
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 6

要获取变量产品的子代版本ID,请使用WC_product get_children()方法 (该方法不具有/不允许任何参数)

// (if needed) Get an instance of the WC_product object (from a dynamic product ID)
$product = wc_get_product($product_id);

// Get children product variation IDs in an array
$children_ids = $product->get_children();

// Get the first ID value
$children_id = reset($children_ids); 
// or 
$children_id = $children_ids[0];
Run Code Online (Sandbox Code Playgroud)

经过测试和工作。