Timeout when trying to add product with function.
-
Hello! I’m currently writing a function which will add products to my cart using a snippet provided in another example here. I’m using the latest version of WC, and I am running it on my local WAMP server.
I run the function, but when I do, I get a “The connection to localhost was interrupted.”. The function is as follows:
function wccustomaddtocart($product) { $alreadyincart = false; $cart = WC()->cart->getcart(); foreach( $cart as $cartitemkey => $values ) { $product = $values['data']; if( $product == $_product->id ) { $already_in_cart = true; break; } } if( ! $already_in_cart ) { WC()->cart->add_to_cart( $product, 1 ); } }If I comment out the WC()->cart->addtocart( $product, 1 ); line, it does not time out, but obviously it doesn’t add the product either. The code itself looks sound to me. Is there any known issue with running addtocart on a localhost? Is there anything else that I can try to add my products? I am able to add to the cart products normally through WooCommerce, so this issue seems isolated to the function.
Thank you!
The topic ‘Timeout when trying to add product with function.’ is closed to new replies.