Today I updated to the latest Woocommerce version and my website suddenly stoppped working. After some inspection, I found the latest update is crashing with a snippet I’m currently using to add items automatically on user visit which I found here:
http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
Is there any aternative snippet that could work insted of this one?