Tips / Recommendations

Automatically add product to cart on visit woocommerce

/**
 * Automatically add product to cart on visit
 */
add_action('template_redirect', 'add_product_to_cart');
function add_product_to_cart()
{
  $simple_product_id = 567;


  if (!is_admin()) {
    $product_id = 567; //replace with your own product id
    $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->get_id() == $product_id)
          $found = true;
      }
  
        // товар с ID 27 в корзине
        // 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);
        
    
    }
  }
}