/** * Action to perform on accept/skip offer */ function so_process_offer_action() { global $current_user, $sa_smart_offers; $so_offer = new SO_Offer(); $so_offers = new SO_Offers(); if (isset($_GET['so_action']) && ($_GET['so_action'] == "accept" || $_GET['so_action'] == "skip")) { $current_offer_id = $_GET['so_offer_id']; $source = !empty($_GET['source']) ? $_GET['source'] : null; list($where, $where_url) = $so_offers->get_page_details(); $page = $where . '_page'; list($accepted_session_variable, $accepted_ids_in_session) = $so_offers->get_accepted_offer_ids_from_session(); list($skipped_session_variable, $skipped_ids_in_session) = $so_offers->get_skipped_offer_ids_from_session(); $skip_offer_id_variable = $where == "any" ? str_replace(array('/', '-', '&', '=', ':'), '', $where_url) . '_skip_offer_id' : $where . '_skip_offer_id'; list($offer_id_on_skipping, $skipped_offer_id_variable) = $so_offers->get_offer_id_on_skipping($skip_offer_id_variable); $parent_offer_id_variable = $where == "any" ? str_replace(array('/', '-', '&', '=', ':'), '', $where_url) . '_parent_offer_id' : $where . '_parent_offer_id'; $check_parent_offer_id_set_or_not = SO_Session_Handler::check_session_set_or_not($parent_offer_id_variable); if (!$check_parent_offer_id_set_or_not) { SO_Session_Handler::so_set_session_variables($parent_offer_id_variable, $current_offer_id); } if ($_GET['so_action'] == "accept") { $variation_data = isset($_POST['variation_id']) || isset($_POST['quantity']) ? $_POST : array(); $parent_offer_id = ''; if ($offer_id_on_skipping != '') { $check_parent_offer_id = SO_Session_Handler::check_session_set_or_not($parent_offer_id_variable); $parent_offer_id = $check_parent_offer_id ? SO_Session_Handler::so_get_session_value($parent_offer_id_variable) : ''; } SO_Session_Handler::so_delete_session($parent_offer_id_variable); SO_Session_Handler::so_delete_session($skip_offer_id_variable); SO_Session_Handler::so_set_session_variables('sa_smart_offers_accepted_offer_ids', $current_offer_id); // Update stats $so_offer->update_accept_skip_count($current_offer_id, 'accepted'); // validate offer before add to cart. $offer_ids = array($current_offer_id); $is_valid = $this->is_offer_valid($page, $offer_ids); if (!empty($is_valid) && is_array($is_valid)) { // Adds to cart $so_offer->action_on_accept_offer($current_offer_id, $page, $parent_offer_id, $variation_data); } else { // display notice $sa_smart_offers->wc_add_notice(__('This is not valid offer for you.', SA_Smart_Offers::$text_domain), 'error'); } } elseif ($_GET['so_action'] == "skip") { $so_offer->update_accept_skip_count($current_offer_id, 'skipped'); // Update if this offer needs to be skipped permanently for this user $skip_permanently = get_post_meta($current_offer_id, 'sa_smart_offer_if_denied_skip_permanently', true); if (!empty($skip_permanently) && $skip_permanently == true && $current_user->ID != 0) { $customer_skipped_offers = $this->get_skipped_offers($current_offer_id); $customer_skipped_offers = array_unique($customer_skipped_offers); update_user_meta($current_user->ID, 'customer_skipped_offers', $customer_skipped_offers); } // To store skipped offers in session even if they are updated in DB SO_Session_Handler::so_set_session_variables('sa_smart_offers_skipped_offer_ids', $current_offer_id); SO_Session_Handler::so_delete_session($skip_offer_id_variable); $redirecting_option = get_post_meta($current_offer_id, 'sa_smart_offer_if_denied', true); $redirect_to = get_post_meta($current_offer_id, 'url', true); if (strpos($where_url, 'so_action=skip')) { $where_url = esc_url(remove_query_arg(array('so_action', 'so_offer_id', 'source'), $where_url)); } ob_clean(); if (empty($redirecting_option)) { wp_safe_redirect($where_url); } elseif (!empty($source)) { if ($page == "checkout_page" && $source == 'so_post_checkout') { $form_values = SO_Session_Handler::check_session_set_or_not('so_checkout_form_data'); if ($form_values) { $sa_so_form_checkout = SO_Session_Handler::so_get_session_value('so_checkout_form_data'); } else { $sa_so_form_checkout = null; } if (!empty($sa_so_form_checkout)) { $_POST = $sa_so_form_checkout; } if (wc_get_page_id('terms') > 0) { $_POST['terms'] = 'yes'; } if ($sa_smart_offers->is_wc_gte_21()) { wc_clear_notices(); } else { $sa_smart_offers->global_wc()->clear_messages(); } $woocommerce_checkout = $sa_smart_offers->global_wc()->checkout(); $woocommerce_checkout->process_checkout(); } elseif ($page == "checkout_page" && $source == 'so_pre_checkout') { wp_safe_redirect($where_url); } } else { if ($redirecting_option == 'order_page') { wp_safe_redirect($where_url); } elseif ($redirect_to != "") { if ($redirecting_option == "offer_page") { $so_offer->force_show_smart_offers($redirect_to); } elseif ($redirecting_option == "url") { if (!preg_match("~^(?:ht)tps?://~i", $redirect_to)) { $return_url = @$_SERVER["HTTPS"] == "on" ? "https://" : "http://"; $return_url = "http://" . $redirect_to; } else { $return_url = $redirect_to; } wp_redirect($return_url); } elseif ($redirecting_option == "particular_page") { wp_safe_redirect(get_permalink($redirect_to)); } } } exit; } } }
function smart_offers_generate_embed_offer() { global $sa_smart_offers; check_ajax_referer('so_generate_embed_offer', 'security'); if (empty($_POST['post_id']) || $_POST['action'] != 'generate_embed_offer') { return; } $so_offer = new SO_Offer(); $data = array(); $offer_id = $_POST['post_id']; $where_url = get_option('siteurl'); $offer_content = $so_offer->return_post_content($offer_id, $page = '', $where_url); $data['content'] = apply_filters('the_content', $offer_content); $button_style = get_option('so_accept_button_styles'); if ($button_style == 'smart_offers_custom_style_button') { $accept_css = get_option('so_css_for_accept'); $style_for_accept_text = ""; } else { $accept_css = get_option($button_style); $style_for_accept_text = "div.so_accept a { text-decoration: none !important; color: white; }"; } $skip_css = get_option('so_css_for_skip'); $style_for_accept = "div.so_accept { {$accept_css} }"; $style_for_skip = "div.so_skip { {$skip_css} }"; $data['style'] = $style_for_accept . $style_for_skip . $style_for_accept_text; echo json_encode($data); die; }
/** * 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; }
/** * 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; }