Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. // add item to cart on visit
  2. add_action( 'template_redirect', 'add_product_to_cart' );
  3. function add_product_to_cart() {
  4.     if ( ! is_admin() ) {
  5.         $product_ids = [385, 386];
  6.  
  7.         //check if product already in cart
  8.         if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
  9.  
  10.             foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  11.                 unset( $product_ids[array_search( $_product->id, $product_ids )] );
  12.             }
  13.  
  14.         }
  15.  
  16.         if ( count($product_ids) > 0 ) {
  17.             foreach($product_ids as $i => $$product_id) {
  18.                 WC()->cart->add_to_cart( $product_id );
  19.             }
  20.         }
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement