Show Gift Items Once Total is Reached

Function created to pull cart total and show different gift items to encourage spending for better rewards. Gift items assigned by product ID array –“$n_gifts = array(181552,181554,177330,181559,181655)”
Active gifts

Gift items change at intervals – $25+, $50+ and $100+. This can be adjusted if needed “if ($cart_total > 25 && $cart_total < 50){" Under the active gifts function will show gifts at the next tier “Spend XX more and select from these items”.Next level gifts

Once a gift is selected it is automatically add product to cart and apply promo code for conversion tracking.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
add_action('woocommerce_after_cart_totals', 'apply_free_gift_coupon');

function apply_free_gift_coupon( ) {
 global $woocommerce;
  $cart_total =  $woocommerce->cart->cart_contents_total;
  $remaining_amt = 50 - $cart_total;
  $remaining_amt  = '$'.money_format('%i', $remaining_amt);
  $coupon = 'gift';
  $product = $cart_item['data'];
 
  echo '<div class="coupon_gift_container">';
  if ($cart_total > 25 && (!in_array($coupon, $woocommerce->cart->get_applied_coupons()))){echo '<h1>Congrats!</h1><p> Your order qualifies for a free gift. Please select your gift below:</p>';
      if ($cart_total > 25 && $cart_total < 50){
      //SMALL
       $pID = array(10447,10678,181343,10673,9675);
        $amt = 50 - $cart_total;
        $amt = '$'.money_format('%i', $amt);
       
       //next level gifts
       $n_gifts = array(181552,181554,177330,181559,181655);
       
       
       }elseif($cart_total > 50 && $cart_total < 99){
      // MEDIUM
      $pID = array(181552,181655,181554,177330,181559);
      $amt = 99 - $cart_total;
      $amt = '$'.money_format('%i', $amt);
       
       //next level gifts
       $n_gifts = array(11701,181576,181552,9732,181559);
       
       
      }elseif($cart_total > 99){         
      //LARGE
      $pID = array(11701,181576,181552,9732,181559);
     
      }

           echo '<ul>';
              foreach ($pID as $product) {
                $product_meta = get_post_meta($product);
                 $pimg = wp_get_attachment_image(($product_meta['_thumbnail_id'][0]), 'full' );
                 $pname = wc_get_product( $product)->get_name();
                 $purl = '/cart/?add-to-cart='.$product;

                 echo '<li class="gift-p">'.$pimg.'<br>';
                 echo '<b>'.$pname.'</b><br>';
                 echo '<a href="'.$purl.'" class="add-gift">Add Gift</a></li>';
                 
                 //echo $product;
              }
       
             
              if ($n_gifts){
                  echo'<div class="clearfloat"></div>';
             echo '</ul><hr><h3>Spend '.$amt.' more to select from the following gift items instead...</h3>';
               echo '<ul>';
              foreach ($n_gifts as $product) {
                $product_meta = get_post_meta($product);
                 $pimg = wp_get_attachment_image(($product_meta['_thumbnail_id'][0]), 'full' );
                 $pname = wc_get_product( $product)->get_name();
                 $purl = '/cart/?add-to-cart='.$product;

                 echo '<li class="gift-p">'.$pimg.'<br>';
                 echo '<b>'.$pname.'</b><br>';
                 
                 //echo $product;
              }
             echo '</ul>';
              }
             
     
     
      //if in gift category add coupon
      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
       $product = $cart_item['data'];
        if(has_term( 'giftcard', 'product_cat', $cart_item['product_id']) && (!WC()->cart->has_discount('gift'))){ //bianca aauto apply discount and coupon code.
        }
    }

  }else{ }
       echo '</div>';
     
   
}


add_action( 'woocommerce_after_calculate_totals', 'woocommerce_B_update_price', 99 );
function woocommerce_B_update_price() {
global $woocommerce;
global $total_discount;
$coupon = 'gift';
$cart_data =  WC()->cart->get_cart();
$cart_total =  $woocommerce->cart->cart_contents_total;


  //SMALL
     if ($cart_total > 25 && $cart_total < 50){
       $pID = array(10447,10678,181343,10673,9675);
    }elseif($cart_total > 50 && $cart_total < 99){
    // MEDIUM
      $pID = array(181552,181554,177330,181559, 181655);
    }elseif($cart_total > 99){
    //LARGE
      $pID = array(11701,181576,181552,9732,181559);
     
    }
 
//var_dump($cart_data );
$i = 0;

foreach ($cart_data as $cart_item_key => $cart_item ) {
    $price = get_post_meta($cart_item['product_id'] , '_price', true);
    $qty = $cart_item['quantity'];
    $cart_item_id = $cart_item['product_id'];

    //check that coupon is applied
    if(in_array($coupon, $woocommerce->cart->get_applied_coupons())){
       
      //check for sale category only discount qty 1
        if(has_term( 'giftcard', 'product_cat', $cart_item['product_id']) ){
            $i++;
           
            //use i to only apply discount to first qualifing item
                 if ($qty <= 1 && $i === 1 && (in_array($cart_item_id, $pID))){

                        $cart_item['data']->set_price('0');
                     
                 }
                 /*
                 if ($qty > 1){
                    $ptotal = $qty * $price;
                    $dis_price = $ptotal - $price;
                                                                             }*/

                     
                           
            }              
           
        }
   
    }

   
}


//if gift in cart auto apply coupon
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
    global $woocommerce;
   
    $cart_data =  WC()->cart->get_cart();

        //$25
         $pID = array(10447,10678,181343,10673,9675/*medium*/,181552,181554,177330,181559,181576/*lg*/,11701,181576,9732,181655);
        foreach ($cart_data as $cart_item_key => $cart_item ) {
            $cart_pID = $cart_item['product_id'];
            if (in_array($cart_pID, $pID) && (!in_array('gift', $woocommerce->cart->get_applied_coupons()))){

                  $woocommerce->cart->add_discount( 'gift' );
                }
        }
       

}


add_action( 'woocommerce_cart_totals_before_order_total', 'add_row' , 99);
function add_row() {
global $woocommerce;
global $total_discount;
$coupon = 'gift';

    if(in_array($coupon, $woocommerce->cart->get_applied_coupons())){
        echo '
        <tr>
            <th>Free Gift!</th>
        <td> <a href="/cart/?remove_coupon=gift">Cancel</a></td>
        </tr>'
;


    }
}

Developed with PHP and SQL