/** * Return valid offers after validating aganist rules based on cart/order details and user details */ function validate_offers($page, $page_offers_id, $details) { global $sa_smart_offers; $user_cart_contains = isset($details['offer_rule_cart_contains']) ? explode(",", $details['offer_rule_cart_contains']) : array(); $user_has_bought = isset($details['offer_rule_has_bought']) ? explode(",", $details['offer_rule_has_bought']) : array(); $cart_category_details = isset($details['offer_rule_cart_category_details']) ? $details['offer_rule_cart_category_details'] : array(); $valid_offers_id = array(); foreach ($page_offers_id as $offer_id => $value) { $valid_offers_id[] = $offer_id; foreach ($value as $rule_key => $rule_value) { if ($rule_key == "offer_rule_category_amount" || $rule_key == "offer_rule_category_total" || $rule_key == "offer_rule_cart_quantity" || $rule_key == "offer_rule_quantity_total") { continue; } $bool = false; switch ($rule_key) { case "default_rule_show_offer": $bool = true; break; case "offer_rule_total_ordered_less": if (isset($details['offer_rule_order_total']) && $details['offer_rule_order_total'] <= $rule_value) { $bool = true; } break; case "offer_rule_total_ordered_more": if (isset($details['offer_rule_order_total']) && $details['offer_rule_order_total'] >= $rule_value) { $bool = true; } break; case "offer_rule_cart_total_less": if (isset($details['offer_rule_total']) && $details['offer_rule_total'] <= $rule_value) { $bool = true; } break; case "offer_rule_cart_total_more": if (isset($details['offer_rule_total']) && $details['offer_rule_total'] >= $rule_value) { $bool = true; } break; case "offer_rule_cart_grand_total_less": if (isset($details['offer_rule_grand_total']) && $details['offer_rule_grand_total'] <= $rule_value) { $bool = true; } break; case "offer_rule_cart_grand_total_more": if (isset($details['offer_rule_grand_total']) && $details['offer_rule_grand_total'] >= $rule_value) { $bool = true; } break; case "offer_rule_registered_period": switch ($rule_value) { case "one_month": if (isset($details['offer_rule_registered_period']) && $details['offer_rule_registered_period'] < 1) { $bool = true; } break; case "three_month": if (isset($details['offer_rule_registered_period']) && $details['offer_rule_registered_period'] < 3) { $bool = true; } break; case "six_month": if (isset($details['offer_rule_registered_period']) && $details['offer_rule_registered_period'] < 6) { $bool = true; } break; case "less_than_1_year": if (isset($details['offer_rule_registered_period']) && $details['offer_rule_registered_period'] < 12) { $bool = true; } break; case "more_than_1_year": if (isset($details['offer_rule_registered_period']) && $details['offer_rule_registered_period'] > 12) { $bool = true; } break; } break; case "offer_rule_registered_user": if (isset($details['offer_rule_registered_user']) && $details['offer_rule_registered_user'] == $rule_value) { $bool = true; } break; case "offer_rule_user_role": if (isset($details['offer_rule_user_role']) && $details['offer_rule_user_role'] == $rule_value) { $bool = true; } break; case "offer_rule_user_role_not": if (isset($details['offer_rule_user_role']) && $details['offer_rule_user_role'] != $rule_value) { $bool = true; } break; case "offer_rule_cart_contains": $rule_cart_contains = explode(",", $rule_value); if (array_intersect($rule_cart_contains, $user_cart_contains)) { if (!empty($value['offer_rule_cart_quantity'])) { $cart = $sa_smart_offers->global_wc()->cart->get_cart(); $product_and_quantity = $this->get_product_id_to_quantity($cart); if ($value['offer_rule_quantity_total'] == "quantity_total_less") { if ($product_and_quantity[$rule_value] <= $value['offer_rule_cart_quantity']) { $bool = true; } } elseif ($value['offer_rule_quantity_total'] == "quantity_total_more") { if ($product_and_quantity[$rule_value] >= $value['offer_rule_cart_quantity']) { $bool = true; } } } else { $bool = true; } } else { $bool = false; } break; case "offer_rule_cart_doesnot_contains": $rule_cart_doesnot_contains = explode(",", $rule_value); $cart_doesnot_contain_val = count(array_intersect($rule_cart_doesnot_contains, $user_cart_contains)) == 0 ? 1 : 0; if ($cart_doesnot_contain_val == 1) { $bool = true; } break; case "offer_rule_has_bought": $rule_has_bought = explode(",", $rule_value); $user_bought_val = count(array_intersect($rule_has_bought, $user_has_bought)) == count($rule_has_bought) ? 1 : 0; if ($user_bought_val == 1) { $bool = true; } break; case "offer_rule_not_bought": $rule_not_bought = explode(",", $rule_value); $user_not_bought_val = count(array_intersect($rule_not_bought, $user_has_bought)) == 0 ? 1 : 0; if ($user_not_bought_val == 1) { $bool = true; } break; case "offer_rule_offer_valid_between": if (isset($rule_value['offer_valid_from']) && !empty($rule_value['offer_valid_from'])) { if (current_time('timestamp') >= $rule_value['offer_valid_from']) { $bool = true; if (isset($rule_value['offer_valid_till']) && !empty($rule_value['offer_valid_till'])) { if (current_time('timestamp') <= $rule_value['offer_valid_till']) { $bool = true; } else { $bool = false; } } } } break; case "offer_rule_cart_prod_categories_is": $rule_contains_categories = $rule_value; if (array_key_exists($rule_contains_categories, $cart_category_details)) { if (!empty($value['offer_rule_category_amount'])) { $cart_amount = $cart_category_details[$rule_contains_categories]; if ($value['offer_rule_category_total'] == "category_total_less") { if ($cart_amount <= $value['offer_rule_category_amount']) { $bool = true; } } elseif ($value['offer_rule_category_total'] == "category_total_more") { if ($cart_amount >= $value['offer_rule_category_amount']) { $bool = true; } } } else { $bool = true; } } else { $bool = false; } break; case "offer_rule_cart_prod_categories_not_is": $rule_not_contains_categories = $rule_value; if (!empty($cart_category_details) && !array_key_exists($rule_not_contains_categories, $cart_category_details)) { $bool = true; } else { $bool = false; } break; } if ($bool === false) { $key = array_search($offer_id, $valid_offers_id); unset($valid_offers_id[$key]); break 1; } } } $validated_offers_id = array(); foreach ($valid_offers_id as $id) { if ($id != '') { $so_offer = new SO_Offer(); $offer_price = $so_offer->get_offer_price(array('offer_id' => $id)); $validated_offers_id[$id] = $offer_price; } } return $validated_offers_id; }
/** * Add offered price in cart. */ function get_offered_cart_item_from_session($cart_item, $values, $key = null) { if (isset($values['smart_offers'])) { $so_offer = new SO_Offer(); $cart_item['smart_offers'] = $values['smart_offers']; $product_id = isset($cart_item['variation_id']) && $cart_item['variation_id'] != '' ? $cart_item['variation_id'] : $cart_item['product_id']; $offer_id = $values['smart_offers']['offer_id']; $price = $so_offer->get_offer_price(array('offer_id' => $offer_id, 'prod_id' => $product_id)); $cart_item['data']->price = $price; $cart_item['data']->sale_price = $price; $cart_item['data']->regular_price = $price; } return $cart_item; }
/** * Shortcode to show price in offer description (Simple Products) */ function shortcode_for_showing_price() { global $post, $product, $sa_smart_offers; if ($post->post_type != 'smart_offers') { return; } $target_product_id = get_post_meta($post->ID, 'target_product_ids', true); $product = $sa_smart_offers->get_product($target_product_id); $product_id = !empty($product->variation_id) ? $product->variation_id : $product->id; if (!is_a($product, 'WC_Product')) { return; } $sale_price = $product->get_sale_price(); $price = $product->get_price(); $so_offer = new SO_Offer(); $offer_price = $so_offer->get_offer_price(array('offer_id' => $post->ID, 'prod_id' => $target_product_id)); if ($sale_price != $offer_price) { $so_display_price_html = '<del>' . $product->get_price_html() . '</del> <ins>' . $sa_smart_offers->wc_price($offer_price) . '</ins>'; } else { $so_display_price_html = $product->get_price_html(); } $price_content = '<span class="price"> ' . __('Offer Price', SA_Smart_Offers::$text_domain) . ': ' . $so_display_price_html . '</span>'; return $price_content; }