Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement