title: Auto add a Product to Cart in Woocommerce 3
body: <p>I would like that anybody who visits my Woocommerce shop finds a free product in his cart. I found this code and it works <strong>but it shows many php errors</strong> in logs.</p>
  <p>Do you have an idea why is not &quot;clean&quot;?</p>
  <p>Any help on this please.</p>
  <pre><code>/*
   * AUTOMATICALLY ADD A PRODUCT TO CART ON VISIT
   */
  function aaptc_add_product_to_cart() {
      if ( ! is_admin() ) {
          $product_id = 99999;  // Product Id of the free product which will get added to cart
          $found  = false;
          //check if product already in cart
          if ( sizeof( WC()-&gt;cart-&gt;get_cart() ) &gt; 0 ) {
              foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
                  $_product = $values['data'];
                  if ( $_product-&gt;get_id() == $product_id )
                      $found = true;
              }
              // if product not found, add it
              if ( ! $found )
                  WC()-&gt;cart-&gt;add_to_cart( $product_id );
          } else {
              // if no products in cart, add it
              WC()-&gt;cart-&gt;add_to_cart( $product_id );
          }
      }
  }
  add_action( 'init', 'aaptc_add_product_to_cart' );
  </code></pre>