duai4512
2018-04-22 12:02
浏览 100
已采纳

从Woocommerce 3中的变量产品中获取变体ID

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 ) 

图片转代码服务由CSDN问答提供 功能建议

必须使用键[0]从数组中获取值,但数组在对象中 。 我怎样才能把它放在变量中?

  WC_Product_Variable Object([children:protected] => Array([0] => 344 [1] => 345)[visible_children:protected] => 数组([0] => 344 [1] => 345)
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhang1852 2018-04-22 13:06
    已采纳

    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.

    点赞 评论
  • doufan1899 2018-04-22 12:23

    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);
    
    点赞 评论

相关推荐 更多相似问题