1
votes

In WooCommerce, I have initially built this function to return options for me to make selections of products to my cart on one page without redirecting to product-single page:

/**
 * [browns_add_to_cart Add to Cart Button function - filtering if simple or grouped or variable product -> button]
 * @return
 */
function brown_foods_variables_add_to_cart(){

    global $product;

    $link = array(
        'url'   => '',
        'label' => '',
        'class' => ''
    );
    switch ( $product->get_type() ) {
        case "variable" :
            $link['url']    = apply_filters( 'woocommerce_variable_add_to_cart', get_permalink( $product->get_id() ) );
            $link['label']  = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
        break;
        case "grouped" :
            $link['url']    = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->get_id() ) );
            $link['label']  = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
        break;
        case "external" :
            $link['url']    = apply_filters( 'external_add_to_cart_url', get_permalink( $product->get_id() ) );
            $link['label']  = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
        break;
        default :
            if ( $product->is_purchasable() ) {
                $link['url']    = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
                $link['label']  = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
                $link['class']  = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
            } else {
                $link['url']    = apply_filters( 'not_purchasable_url', get_permalink( $product->get_id() ) );
                $link['label']  = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
            }
        break;
    }
    // If there is a simple product.
    if ( $product->get_type() == 'simple' ) {  
        ?>
        <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="simple-cart cart" method="post" enctype="multipart/form-data">
        <?php
        // Displays the quantity box.
        woocommerce_quantity_input();

        if ( $price_html = $product->get_price_html() ) : 
            ?>
            <span class="food-price"><?php echo $price_html; ?></span>
            <?php 
        endif;

        // Display the submit button.
        echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" 
                                    data-quantity="1" class="%s button product_type_simple">%s</button>', 
            esc_attr( $product->get_id() ), 
            esc_attr( $product->get_sku() ), 
            esc_attr( $link['class'] ), 
            esc_html( $link['label'] ) ); 
        ?>
    </form>

    <?php } 

    elseif ($product->get_type() == 'grouped') {

        require get_template_directory() . '/woocommerce/single-product/add-to-cart/grouped.php';

    }
    else 
    {
        echo apply_filters( 'woocommerce_loop_add_to_cart_link',
            sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                esc_attr( $product->get_id() ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() )
            ),
        $product );  
    }
}

However when the product is grouped, product options to do not return with the else statement thus I required grouped.php elseif ($product->get_type() == 'grouped') this returns an error:

Warning: Invalid argument supplied for foreach() in .....\woocommerce\single-product\add-to-cart\grouped.php.

Any help to get me to return options for grouped products.

1

1 Answers

1
votes

You should try to replace this code:

    <?php } 

    elseif ($product->get_type() == 'grouped') {

        require get_template_directory() . '/woocommerce/single-product/add-to-cart/grouped.php';

    }
    else 
    {

By this:

    <?php } 

    elseif ($product->get_type() == 'grouped') {

        foreach( $product->get_children() as $children_id ){
            $grouped_products[] = wc_get_product($children_id);
        }

        wc_get_template( 'single-product/add-to-cart/grouped.php', array('grouped_products' => $grouped_products ));

    }
    else 
    {

As the missing argument in the foreach loop is certainly $grouped_products.