= strtotime($fromdate)) && empty($todate)) { $return = true; } elseif ((strtotime($now) >= strtotime($fromdate)) && (strtotime($now) <= strtotime($todate))) { $return = true; } else { $return = false; } return $return; } } $product_array = array(); global $woocommerce; $cart_item_data = $woocommerce->cart->cart_contents; foreach ($cart_item_data as $key => $hash) { if (strpos($key, 'free') !== false) { //remove free products $woocommerce->cart->remove_cart_item($key); continue; } if (isset($hash['variation_id']) && $hash['variation_id'] != 0) { $pid = $hash['variation_id']; } else { $pid = $hash['product_id']; } $quantity = $hash['quantity']; if (isset($product_array[$pid]) && is_numeric($product_array[$pid]) && $product_array[$pid] > 0 && strpos($key, 'free') === false) { $product_array[$pid] += $quantity; } elseif (strpos($key, 'free') === false) { $product_array[$pid] = $quantity; } } $dummy_option = array('buy_get_free_rules' => array(),); $rules_option_array = get_option('xa_dp_rules', $dummy_option); if (!isset($rules_option_array['buy_get_free_rules'])) { $rules_option_array['buy_get_free_rules'] = array(); } $rules = $rules_option_array['buy_get_free_rules']; if (!function_exists("cmpBySort")) { function cmpBySort($a, $b) { $a_total_purchased = 0; $a_total_free = 0; $b_total_purchased = 0; $b_total_free = 0; $a_total = 0; $b_total = 0; //print_r($a); foreach ($a['purchased_product_id'] as $pid => $qnty) { $a_total_purchased += $qnty; } foreach ($a['free_product_id'] as $pid => $qnty) { $a_total_free += $qnty; } foreach ($b['purchased_product_id'] as $pid => $qnty) { $b_total_purchased += $qnty; } foreach ($b['free_product_id'] as $pid => $qnty) { $b_total_free += $qnty; } $a_total = floatval($a_total_purchased) / floatval($a_total_free); $b_total = floatval($b_total_purchased) / floatval($b_total_free); return floatval($a_total) - floatval($b_total); } } usort($rules, 'cmpBySort'); $running_pr = $product_array; foreach ($rules as $rno => $rule) { if (check_date_range_and_roles($rule) != true) { continue; } $not_in_cart = array(); $in_cart = array(); extract($rule); $min_valid_qnty = array(); foreach ($purchased_product_id as $pid => $qnty) { if (isset($running_pr[$pid]) && $running_pr[$pid] >= $qnty) { $in_cart[$pid] = $running_pr[$pid]; $min_valid_qnty[] = intval($running_pr[$pid] / $qnty); } else { $not_in_cart[$pid] = $qnty; continue(2); } } $minv = intval(min($min_valid_qnty)); $free_to_add = array(); if ($minv >= 1) { foreach ($free_product_id as $pid => $qnty) { if (isset($free_to_add[$pid])) { $free_to_add[$pid] += $qnty * $minv; } else { $free_to_add[$pid] = $qnty * $minv; } } if (get_settings_status('auto_add_free_product_on_off') == 'enable') { // only works for different products foreach ($free_to_add as $pid2 => $qnty2) { $product_data = wc_get_product($pid2); if (empty($pid2) || empty($product_data)) { continue; } if (isset($adjustment) && is_numeric($adjustment)) { $product_data->set_price($adjustment); } else { $product_data->set_price(0.0); } $cart_item_key = "free" . md5($pid2); $woocommerce->cart->cart_contents[$cart_item_key] = array( 'product_id' => $pid2, 'variation_id' => 0, 'variation' => array(), 'quantity' => $free_to_add[$pid2], 'data' => $product_data ); if (isset($running_pr[$pid2])) { @@$running_pr[$pid2] = $running_pr[$pid2] - ($purchased_product_id[$pid2] * $minv); } if (isset($in_cart[$pid2])) { $in_cart[$pid2] = $in_cart[$pid2] - ($purchased_product_id[$pid2] * $minv); } unset($free_to_add[$pid2]); } } else { foreach ($woocommerce->cart->cart_contents as $key => $value) { if (in_array($value['product_id'], array_keys($free_to_add))) { $pid = $value['product_id']; /// add as free $product_data = wc_get_product($pid); if (empty($pid) || empty($product_data)) { continue; } if (isset($adjustment) && is_numeric($adjustment)) { $product_data->set_price($adjustment); } else { $product_data->set_price(0.0); } $cart_item_key = "free" . md5($pid2); $woocommerce->cart->cart_contents[$cart_item_key] = array( 'product_id' => $pid, 'variation_id' => 0, 'variation' => array(), 'quantity' => $free_to_add[$pid], 'data' => $product_data ); $qnty_to_reduce = $free_to_add[$pid]; if ($running_pr[$pid] < $qnty_to_reduce) { $qnty_to_reduce = $running_pr[$pid]; } if ($running_pr[$pid] == 1 && !in_array($pid, array_keys($purchased_product_id))) { $woocommerce->cart->set_quantity($key, 0, false); } elseif ($running_pr[$pid] == 1) { $woocommerce->cart->set_quantity($key, 1, false); } if (isset($running_pr[$pid])) { $running_pr[$pid] = $running_pr[$pid] - $free_to_add[$pid]; } if (isset($in_cart[$pid])) { $in_cart[$pid] = $in_cart[$pid] - $free_to_add[$pid]; } $free_to_add[$value['product_id']] = 0; $tmppid = $value['product_id']; unset($free_to_add[$tmppid]); } } } } //end if($minv>=1) else { continue; } }