Please submit a private ticket if you need to share sensitive information, such as license details and admin credentials.

Okay
  Public Ticket #2584251
Query bundles that contain specific products
Closed

Comments

  • Davide Golzi started the conversation

    Hi, i'm interested to create a shortcode able to display bundles that contains a specific product.


    Can you help me write the query to retrive the bundle product id giving the must contained product id?

  •  767
    Janilyn replied

    Hi Davide,

    Thank you for contacting WPClever Support Forum. 

    We’ve received your ticket and assigned one of our developers to help you with that. 

    He’ll be looking into your question and responding with specific instructions as soon as possible. 

    Please stay patient as we're having very heavy workloads, it might take some time for our developers to reach back.

    Best regards.

    Janilyn T. - WPClever Support Agent

  •  1,226
    Dustin replied

    Hi Davide,

    Please try this function:

    function woosb_get_bundles( $product_id, $per_page = 500, $offset = 0 ) {
        $bundles        = array();
        $product_id_str = $product_id . '/';
        $query_args     = array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => $per_page,
            'offset'         => $offset,
            'tax_query'      => array(
                array(
                    'taxonomy' => 'product_type',
                    'field'    => 'slug',
                    'terms'    => array( 'woosb' ),
                    'operator' => 'IN',
                )
            ),
            'meta_query'     => array(
                array(
                    'key'     => 'woosb_ids',
                    'value'   => $product_id_str,
                    'compare' => 'LIKE',
                )
            )
        );
        $query          = new WP_Query( $query_args );
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                $_product = wc_get_product( get_the_ID() );
                if ( ! $_product ) {
                    continue;
                }
                $bundles[] = $_product;
            }
            wp_reset_query();
        }
        return ! empty( $bundles ) ? $bundles : false;
    }

    Best regards,
    Dustin