Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Automatically adds product to cart on visit
- */
- add_action( 'template_redirect', 'add_product_to_cart' );
- function add_product_to_cart() {
- if ( ! is_admin() ) {
- $product_id = 1267; //product added automatically
- $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;
- }
- // if product not found, add it
- if ( ! $found )
- WC()->cart->add_to_cart( $product_id );
- } else {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement