【问题标题】:Get variations IDs from a variable product in Woocommerce 3从 Woocommerce 3 中的可变产品获取变体 ID
【发布时间】:2018-04-22 12:02:41
【问题描述】:

需要从键为[0]的数组中取值,但数组在对象中。我怎样才能把它放在一个变量中?

  WC_Product_Variable Object ( [children:protected] => Array ( [0] => 344 [1] => 345 ) [visible_children:protected] => Array ( [0] => 344 [1] => 345 ) 

【问题讨论】:

    标签: php wordpress woocommerce product children


    【解决方案1】:

    要获取可变产品的子变体 ID,请使用 WC_product get_children() method(没有/允许任何参数)

    // (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];
    

    经过测试并且有效。

    【讨论】:

    • 谢谢。工作。
    • 我有一个问题。提示如何接收例如不是第一个元素,例如2或3。这里已经没有使用函数reset()
    • @AleksPvn $children_ids 是子变体 ID 的一个数组...因此您可以使用例如:$second_children_id = $children_ids[1];$third_children_id = $children_ids[2];...您甚至可以使用@ 987654328@ 循环从每个变体 ID 中获取信息。
    • 你怎么能从中得到标签?
    【解决方案2】:

    您无权访问此对象,因为它受到保护。

    在你的情况下,尝试调用方法get_children()

    WC_Product_Variable::get_children(0);
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2018-07-20
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多