问题
It is necessary to get the value from the array with the key [0], but the array is in the object. How can I put it in a variable?
WC_Product_Variable Object ( [children:protected] => Array ( [0] => 344 [1] => 345 ) [visible_children:protected] => Array ( [0] => 344 [1] => 345 )
回答1:
To get the children variation Ids for a variable product, use WC_product get_children() method (that doesn't have/allow any arguments):
// (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];
Tested and works.
回答2:
You do not have access to this object because it is protected.
In your case, try to call the method get_children()
.
WC_Product_Variable::get_children(0);
来源:https://stackoverflow.com/questions/49965691/get-variations-ids-from-a-variable-product-in-woocommerce-3