Get variations IDs from a variable product in Woocommerce 3

混江龙づ霸主 提交于 2019-12-31 03:06:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!