从Woocommerce 3中的可变产品获取版本ID [英] Get variations IDs from a variable product in Woocommerce 3

查看:101
本文介绍了从Woocommerce 3中的可变产品获取版本ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有必要使用键[0]从数组中获取值,但是数组在对象中。如何将其放入变量中?

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 ) 


推荐答案

要获取变量产品的子代版本ID,请使用 WC_product get_children()方法 (没有/不允许任何参数)

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

经过测试并有效。

这篇关于从Woocommerce 3中的可变产品获取版本ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆