Woocommerce add_to_cart giving me fatal error

I have the follow code to add multiple item to cart using Woocommerce plugin and it give me Fatal error:

Fatal error: Maximum function nesting level of ‘100’ reached, aborting! in …wp-contentpluginswoocommercewoocommerce.php on line 94

Read More

I also try to increase the nesting level but it no help.
Here is the code:

add_action( 'init', 'multi_add_to_cart' );
function multi_add_to_cart() {
    if( !isset( $_POST['multi_add_to_cart_nonce'] )  ) return false;
    if ( !wp_verify_nonce( $_POST['multi_add_to_cart_nonce'], 'multi_add_to_cart_nonce') ) return false;
    global $woocommerce;
    $added_products = 0;
    foreach( $_POST['multi_add_to_cart'] as $variation_id => $variation ) {
        if( (int) $variation['quantity'] > 0 ) {
            $woocommerce->cart->add_to_cart(
                $variation['product_id'],   // string $product_id
                $variation['quantity'],     // string $quantity = 1
                (int) $variation_id,        // integer $variation_id = ''
                ( isset($variation['attributes']) && !empty($variation['attributes']) ) ? $variation['attributes'] : false // Attributes!
            );
           $added_products++;
        }
    }
    if( $added_products > 0 ) woocommerce_add_to_cart_message($variation['product_id'] );
}

Related posts

Leave a Reply

1 comment