Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch child products in grouped products woocommerce

I want to fetch child product ids of a parent grouped product, how to do that, earlier woocommerce saves the child array in wp_option table as key to be _transient_wc_product_children_ids_8 but the recent update has changed the way it saves to the database, plz help me, how can I fetch child products from parent product in grouped products.

like image 535
prayas Avatar asked Sep 22 '14 13:09

prayas


2 Answers

Use the get_children() method in the WC_Product_Grouped class.

$product_id = 8; // ID of parent product
$product    = get_product( $product_id );
$children   = $product->get_children();

It should return an array of product IDs.

UPDATED for WooCommerce 2.5+ Replaces get_product() with wc_get_product()

$product_id = 8; // ID of parent product
$product    = wc_get_product( $product_id );
$children   = $product->get_children();
like image 81
helgatheviking Avatar answered Oct 18 '22 09:10

helgatheviking


On Woocommerce version 2.5.0 get_product is already deprecated.

Use wc_get_product instead

$product_id = 8;
$product = wc_get_product($product_id);
$product->get_children();
like image 23
jameshwart lopez Avatar answered Oct 18 '22 11:10

jameshwart lopez