Support » Plugin: YITH WooCommerce Product Bundles » Showing stock level of bundled products

  • It would be really handy to be able to get the stock level of a bundled product. The function would have to work out the stock level of the bundled product by checking the stock level of the items that make up the bundle.

    I needed this for a website I manage so wrote a function for it.
    I achieved it by adding this to the following file:-

     * Returns bundled stock quantity
     * Sort through the bundled products and work out the maximum
     * possible number of bundles based on stock levels.
     * @return int
     * @author Leanza Francesco <>
    public function get_bundle_stock_quantity() {
        $bundled_items = !empty( $this->bundled_items ) ? $this->bundled_items : false;
        if ( $bundled_items ) {
            $maxPossibleBundles = array();
            foreach ( $bundled_items as $bundled_item ) {
                $stockQty = $bundled_item->get_product()->get_stock_quantity();
                $qtyForOneBundle = $bundled_item->get_quantity();
                $maxPossibleBundles[] = floor($stockQty / $qtyForOneBundle);
            sort($maxPossibleBundles); // sort low to high
        return $maxPossibleBundles[0];

    Then in my template I could do:-

    I actually posted about this 3 years ago:-

    I still need it so every time I update the plugin I just keep adding it back in. Would be really cool if we could add it as part of the core plugin code.


Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author YITHEMES


    Hi Carl,
    and thank you for writing in!

    If you want to not lose your customization on plugin updates, you should keep it out of the plugin.
    To do it, you should simply add the following code snippet to your child theme functions.php

    if ( !function_exists( 'carl_get_bundle_stock_quantity' ) ) {
        function carl_get_bundle_stock_quantity( $_product ) {
            $quantity = false;
            if ( $_product ) {
                $quantity = $_product->get_stock_quantity();
                if ( $_product->is_type( 'yith_bundle' ) ) {
                    $bundled_items = $_product->get_bundled_items();
                    if ( $bundled_items ) {
                        $maxPossibleBundles = array();
                        foreach ( $bundled_items as $bundled_item ) {
                            if ( $bundled_item->get_product()->managing_stock() ) {
                                $stockQty             = $bundled_item->get_product()->get_stock_quantity();
                                $qtyForOneBundle      = $bundled_item->get_quantity();
                                $maxPossibleBundles[] = floor( $stockQty / $qtyForOneBundle );
                        if ( $maxPossibleBundles ) {
                            $quantity = min( $maxPossibleBundles );
            return $quantity;

    After adding that code snippet, you’ll be able to use the carl_get_bundle_stock_quantity function in your template as follows:
    carl_get_bundle_stock_quantity( $_product );

    Please try this solution and let us know if everything works fine!

    Hi Carl,

    I’m having a stock management issue that it looks like you may have the solution above?

    Most of our products we sell in bundles and for each bundle size we naturally have an attribute & Variation with its own SKU created.

    For example – 10 standard sandbags, 25 standard sandbag, 50 standard…100, 250, 500,1000 you get the idea. Its bundles of the same product. No Bom’s or mixing product together.
    Naturally the bigger the bundle the lower the cost per unit.

    But woocommerce reads each attribute / variation / bundle as qty of 1.

    So if i sell the product one sandbag the stock is reduced by 1, if i sell 1 bundle of 500 sandbags the stock is still only reduced by qty 1 – even tho that was one bundle of 500 bags
    Does your code fix this??

    Martin T

    Thread Starter codekipple


    Hi Martin,
    Yeah I think my code will do what you need, you should try the version YITHEMES replied with so you don’t have to edit the plugin.


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Showing stock level of bundled products’ is closed to new replies.