Guest User

Untitled

a guest
Feb 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2.  
  3. // add item to cart on visit
  4. add_action( 'gens_raf_checkout_check', 'add_product_to_cart' );
  5. function add_product_to_cart() {
  6. if (is_admin()) {
  7. return false;
  8. }
  9. $user_info = get_userdata(get_current_user_id());
  10. $user_email = $user_info->user_email;
  11. $args = array(
  12. 'posts_per_page' => -1,
  13. 'post_type' => 'shop_coupon',
  14. 'post_status' => 'publish',
  15. 'meta_query' => array(
  16. 'relation' => 'AND',
  17. array(
  18. 'key' => 'customer_email',
  19. 'value' => $user_email,
  20. 'compare' => 'LIKE'
  21. ),
  22. array(
  23. 'relation' => 'OR',
  24. array(
  25. 'key' => 'usage_count',
  26. 'value' => '0',
  27. 'compare' => 'LIKE'
  28. ),
  29. array(
  30. 'key' => 'usage_count',
  31. 'compare' => 'NOT EXISTS'
  32. )
  33. )
  34. ),
  35. );
  36. $coupons = get_posts( $args );
  37.  
  38. if(empty($coupons)) {
  39. return false;
  40. }
  41. $coupon_title = get_the_title($coupons[0]->ID);
  42. $product_id = 4275;
  43. $found = false;
  44. //check if product already in cart
  45. if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
  46. foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  47. $_product = $values['data'];
  48. if ( $_product->id == $product_id )
  49. $found = true;
  50. }
  51. // if product not found, add it
  52. if ( !$found ){
  53. WC()->cart->add_to_cart( $product_id );
  54. WC()->cart->add_discount( $coupon_title );
  55. }
  56. } else {
  57. // if no products in cart, add it
  58. WC()->cart->add_to_cart( $product_id );
  59. WC()->cart->add_discount( $coupon_title );
  60. }
  61. }
Add Comment
Please, Sign In to add comment