Hooman
Hooman

Reputation: 371

How to get the grouped product ID from a linked product in woocommerce

I have a grouped product product-1 , which has many linked products:

-product-1 (grouped product)

I want to get the ID of product-1 using the ID of product-2

Upvotes: 4

Views: 4005

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254378

You can't get the product Id of a particular grouped product through one of its children product IDs, as each child can be in many different grouped products.

The only data that defines the children products IDS for a grouped product is located in wp_postmeta table arround the meta_key _children as an array of children product IDs.

Now if the children product ID which you want to use to retrieve the parent grouped product ID is only the children of one unique grouped product, you can use the following SQL query embedded in a function:

function get_parent_grouped_id( $children_id ){
    global $wpdb;
    $results = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}postmeta
        WHERE meta_key = '_children' AND meta_value LIKE '%$children_id%'");
    // Will only return one product Id or false if there is zero or many
    return sizeof($results) == 1 ? reset($results) : false;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

USAGE

Here below 738 is one of the children products Id. It can also be a dynamic value passed via a variable.

$parent_grouped_id = get_parent_grouped_id( 738 );

ADDITION - Get all grouped products using a WC_Product_Query:

  1. Array of grouped products Objects:

    $grouped_products = wc_get_products( array( 'limit' => -1, 'type' => 'grouped' ) );

  2. Array of grouped products IDS only:

    $ids = wc_get_products( array( 'limit' => -1, 'type' => 'grouped', 'return' => 'ids' ) );

Upvotes: 6

Andrey Serb
Andrey Serb

Reputation: 11

I've ellaborated a little bit on previous correct answer by @LoicTheAztec and added some caching to it so to avoid multiple database requests. Wrote this for my project, just sharing it with you - don't be harsh on me I'm newby here. Thanks to @LoicTheAztec for his initial answer.

if ( ! function_exists( 'wc_get_parent_grouped_id' ) ) {

function wc_get_parent_grouped_id( $id ){

    global $wpdb;

    $cdata = wp_cache_get( __FUNCTION__, 'woocommerce' );

    if ( ! is_array($cdata) )
        $cdata = array();

    if ( ! isset($cdata[$id]) ) {

        $cdata[$id] = $parent_id = $children = false;

        $qdata = $wpdb->get_row("SELECT post_id, meta_value
                                 FROM $wpdb->postmeta
                                 WHERE meta_key = '_children' 
                                 AND meta_value LIKE '%$id%'");

        if ( is_object($qdata) ) {

            $parent_id = $qdata->post_id;
            $children = $qdata->meta_value;

            if ( is_string($children) )
                $children = unserialize($children);

            if ( is_array($children) && count($children) > 0 )
                foreach ($children as $child_id)
                    $cdata[$child_id] = $parent_id;
        }

        wp_cache_set( __FUNCTION__, apply_filters( __FUNCTION__ . '_filter', $cdata, $id, $parent_id, $children, $qdata ), 'woocommerce' );
    }

    return $cdata[$id];
}

}

Upvotes: 1

Related Questions