Check if product bundle has bundle-upsells ( optional items ) or simple bundled items

43 views Asked by At

I am pretty new to coding and trying to edit our WC website a bit. Hope my English is decent enough to pass the needed information…

I made a function that checks if product is a type- bundle and it works fine. Unfortunately, I need my function to differ between bundles with optional items and bundles without optional bundled items.

-- Edited update: I changed my tactics, since I dont need JS for this check. But, I lack the needed php basics anyways.

add_action ( 'woocommerce_before_add_to_cart_form', 'ino_qty_main_function');

    function ino_qty_main_function() {
        global $product;
        if ( $product->is_type('bundle') ) {
            $hasqty = 5;
                $bundleditems = $product->get_bundled_items();
                foreach ( $bundled_items as $bundleditem ) {
                    if ( $bundleditem->is_optional('yes')) {
                        $hasqty = 1;
                        echo "<h2> 1 product optional </h2>";
                    } else {
                        $hasqty = 0;
                        echo "<h2> 1 product not optional </h2>";
                    }
                }
            echo "<h2> final hasqty" . $hasqty . "</h2>";
        }

    }

this is a function to check how it works [ Not working with IDE, working on staging site ]. I am pretty sure I lack the basic knowledge on how to perform this check.

in the bundle plugin files i found the next things that should help to build this function correctly:

public function is_optional() {
        return 'yes' === $this->optional;
    }

and also the array that stores the data:

private function load_data() {

        // Defaults.
        $defaults = array(
            'quantity_min'                          => 1,
            'quantity_max'                          => 1,
            'quantity_default'                      => 1,
            'priced_individually'                   => 'no',
            'shipped_individually'                  => 'no',
            'override_title'                        => 'no',
            'title'                                 => '',
            'override_description'                  => 'no',
            'description'                           => '',
            'optional'                              => 'no',
            'hide_thumbnail'                        => 'no',
            'discount'                              => '',
            'override_variations'                   => 'no',
            'override_default_variation_attributes' => 'no',
            'allowed_variations'                    => false,
            'default_variation_attributes'          => false,
            'single_product_visibility'             => 'visible',
            'cart_visibility'                       => 'visible',
            'order_visibility'                      => 'visible',
            'single_product_price_visibility'       => 'visible',
            'cart_price_visibility'                 => 'visible',
            'order_price_visibility'                => 'visible',
            'stock_status'                          => null,
            'max_stock'                             => null
        );

What I want to achieve is to check if the product page has bundled items, if it does - to check if at least one of the bundled products is optional. if it is, $hasqty = 1 , else $hasqty=0.

Edit2: Got it to work! And realized I missed a lot of the basics.

add_action ( 'woocommerce_before_add_to_cart_form', 'ino_qty_main_function');

    function ino_qty_main_function() {
        global $product;
        if ( $product->is_type('bundle') ) {
            $hasqty = 0;
                $bundleditems = $product->get_bundled_items();
                foreach ( $bundleditems as $bundleditem ) {
                    if ($bundleditem->is_optional('yes')) {
                
                        $hasqty++ ;
                        echo "<h2> 1 product optional </h2>";
                    }
                }
        
            echo "<h2> final hasqty" . $hasqty . "</h2>";
        }
    }

All that's left is to tweak it to add buttons instead of printing the testing values.

1

There are 1 answers

0
Ino Ingvar On
add_action ( 'woocommerce_before_add_to_cart_form', 'ino_qty_main_function');

    function ino_qty_main_function() {
        global $product;
        if ( $product->is_type('bundle') ) {
            $hasqty = 0;
                $bundleditems = $product->get_bundled_items();
                foreach ( $bundleditems as $bundleditem ) {
                    if ($bundleditem->is_optional('yes')) {
                
                        $hasqty++ ;
                        echo "<h2> 1 product optional </h2>";
                    }
                }
        
            echo "<h2> final hasqty" . $hasqty . "</h2>";
        }
    }

Counts the optional bundled items! Since we are going to use only Simple Bundles or only Optional Bundles - It's enough for me to keep going with rest of the code.

Hope it might also help someone in the future. Works with WooCommerce bundles plugin