2

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-content\plugins\woocommerce\woocommerce.php on line 94

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'] );
}
tjntun
  • 73
  • 1
  • 7
  • that's odd... I can't see any problem with the code.. maybe it's your server... try reading this http://stackoverflow.com/questions/8656089/solution-for-fatal-error-maximum-function-nesting-level-of-100-reached-abor – Reigel Gallarde Mar 18 '15 at 04:02
  • thank you but I've read the article above but it's no help :( both my centos server and my localhost show this error. – tjntun Mar 18 '15 at 04:10
  • 1
    try changing `init` to `template_redirect`... just a guess.. – Reigel Gallarde Mar 18 '15 at 04:30

1 Answers1

2

there might be a lot of functions hooked to init. try changing init to template_redirect.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139