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

从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 ) 
  • 写回答

2条回答 默认 最新

  • douzhang1852 2018-04-22 05: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部