SHARE
TWEET

Untitled

a guest Jun 17th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Automatically adds product to cart on visit
  3.  */
  4. add_action( 'template_redirect', 'add_product_to_cart' );
  5. function add_product_to_cart() {
  6.     if ( ! is_admin() ) {
  7.         $product_id = 1267; //product added automatically
  8.         $found = false;
  9.         //check if product already in cart
  10.         if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
  11.             foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  12.                 $_product = $values['data'];
  13.                 if ( $_product->get_id() == $product_id )
  14.                     $found = true;
  15.             }
  16.             // if product not found, add it
  17.             if ( ! $found )
  18.                 WC()->cart->add_to_cart( $product_id );
  19.         } else {
  20.  
  21.         }
  22.     }
  23. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top