/** * Action to perform when offer is accepted */ function action_on_accept_offer($post_id, $page, $parent_offer_id, $variation_data) { global $current_user, $sa_smart_offers; $source = !empty($_GET['source']) ? $_GET['source'] : null; $so_offers = new SO_Offers(); $current_offer_id = $_GET['so_offer_id']; list($where, $where_url) = $so_offers->get_page_details(); if (strpos($where_url, 'so_action=skip') || strpos($where_url, 'so_action=accept')) { $where_url = esc_url(remove_query_arg(array('so_action', 'so_offer_id', 'source'), $where_url)); } 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 (isset($variation_data['variation_id']) && $variation_data['variation_id'] != '') { $target_product_id = $variation_data['variation_id']; } else { $target_product_id = get_post_meta($post_id, 'target_product_ids', true); } $quantity = isset($variation_data['quantity']) && !empty($variation_data['quantity']) ? $variation_data['quantity'] : 1; if (!empty($target_product_id)) { if (isset($variation_data['variation_id']) && $variation_data['variation_id'] != '') { $all_variations_set = true; $parent_id = $variation_data['parent_prod_id']; $adding_to_cart = $sa_smart_offers->get_product($parent_id); //get_product( $parent_id ); $attributes = $adding_to_cart->get_attributes(); $variation_id = $variation_data['variation_id']; $variation_instance = $sa_smart_offers->get_product($variation_id); //get_product( $variation_id ); foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (!empty($_POST[$taxonomy])) { // Get value from post data // Don't use woocommerce_clean as it destroys sanitized characters $value = sanitize_title(trim(stripslashes($_POST[$taxonomy]))); // Get valid value from variation $valid_value = $variation_instance->variation_data[$taxonomy]; // Allow if valid if ($valid_value == '' || $valid_value == $value) { if ($attribute['is_taxonomy']) { $variation[esc_html($attribute['name'])] = $value; } else { // For custom attributes, get the name from the slug $options = array_map('trim', explode('|', $attribute['value'])); foreach ($options as $option) { if (sanitize_title($option) == $value) { $value = $option; break; } } $variation[esc_html($attribute['name'])] = $value; } continue; } } $all_variations_set = false; } } else { $target_product_instance = $sa_smart_offers->get_product($target_product_id); if (isset($target_product_instance->variation_id)) { $parent_id = $target_product_instance->id; $variation_id = $target_product_instance->variation_id; $variation = $target_product_instance->variation_data; } else { $parent_id = $target_product_instance->id; $variation_id = ''; $variation = ''; } } // Storing offer rules of parent in case of skipped offers if (!empty($parent_offer_id) || ($page == "cart_page" || $page == "checkout_page" || $page == "myaccount_page" || $page == "home_page" || $page == "post_checkout_page" || $page == "any_page")) { $offer_rules = get_post_meta($post_id, '_offer_rules', true); } $action_on_accept = get_post_meta($post_id, 'so_actions_on_accept', true); $products_to_be_removed = array(); if (!empty($action_on_accept)) { if (isset($action_on_accept['remove_prods_from_cart'])) { $products_to_be_removed = !empty($action_on_accept['remove_prods_from_cart']) ? explode(',', $action_on_accept['remove_prods_from_cart']) : array(); } } if (in_array('all', $products_to_be_removed)) { $sa_smart_offers->global_wc()->cart->empty_cart(); $products_to_be_removed = array(); } if (!empty($offer_rules)) { foreach ($offer_rules as $key => $val) { if ($val["offer_action"] == "cart_contains") { $cart_contains = $val['offer_rule_value']; } } $cart_contains = isset($cart_contains) ? explode(",", $cart_contains) : array(); if (!empty($products_to_be_removed)) { foreach ($products_to_be_removed as $prod_ids) { $prod_parent_id = wp_get_post_parent_id($prod_ids); if (!empty($prod_parent_id)) { $products_to_be_removed[] = $prod_parent_id; } } } $cart_contains_item_key = array(); $keys_of_products_removed = array(); if (!empty($cart_contains)) { if (count($products_to_be_removed) > 0) { $cart_contains = array_diff($cart_contains, $products_to_be_removed); } foreach ($cart_contains as $id) { foreach ($sa_smart_offers->global_wc()->cart->cart_contents as $key => $values) { if ($id == $values['product_id'] || $id == $values['variation_id']) { $cart_contains_item_key[] = $key; } } } } if (count($products_to_be_removed) > 0) { foreach ($products_to_be_removed as $p_id) { foreach ($sa_smart_offers->global_wc()->cart->cart_contents as $key => $values) { if ($p_id == $values['product_id'] || $p_id == $values['variation_id']) { $keys_of_products_removed[] = $key; } } } } if (!empty($keys_of_products_removed)) { if (count($cart_contains_item_key) > 0) { $cart_contains_item_key = array_diff($cart_contains_item_key, $keys_of_products_removed); } if (count($cart_contains) < 0) { $cart_contains = array(); } if (count($cart_contains_item_key) < 0) { $cart_contains_item_key = array(); } if (isset($parent_offer_id) && !empty($parent_offer_id)) { $parent_offer_ids = array(); array_push($parent_offer_ids, $parent_offer_id); } else { $parent_offer_ids = array(); } $cart = $sa_smart_offers->global_wc()->cart->cart_contents; foreach ($keys_of_products_removed as $cart_key) { if (isset($cart[$cart_key]['smart_offers'])) { if (is_array($cart[$cart_key]['smart_offers']['cart_contains_keys']) && count($cart[$cart_key]['smart_offers']['cart_contains_keys']) > 0) { $cart_contains_item_key = array_unique(array_merge($cart_contains_item_key, $cart[$cart_key]['smart_offers']['cart_contains_keys'])); } if (is_array($cart[$cart_key]['smart_offers']['cart_contains_ids']) && count($cart[$cart_key]['smart_offers']['cart_contains_ids']) > 0) { $cart_contains = array_unique(array_merge($cart_contains, $cart[$cart_key]['smart_offers']['cart_contains_ids'])); } if (isset($cart[$cart_key]['smart_offers']['parent_offer_id']) && !empty($cart[$cart_key]['smart_offers']['parent_offer_id'])) { if (is_array($cart[$cart_key]['smart_offers']['parent_offer_id'])) { $parent_offer_id = array_unique(array_merge($parent_offer_ids, $cart[$cart_key]['smart_offers']['parent_offer_id'])); } else { array_push($parent_offer_ids, $cart[$cart_key]['smart_offers']['parent_offer_id']); } } } unset($sa_smart_offers->global_wc()->cart->cart_contents[$cart_key]); } if (is_array($parent_offer_ids) && count($parent_offer_ids) > 0) { $parent_offer_id = $parent_offer_ids; } } if (!empty($cart_contains) && is_array($cart_contains) && !empty($cart_contains_item_key)) { $args['smart_offers'] = array('accept_offer' => true, 'offer_id' => $post_id, 'accepted_from' => $page, 'cart_contains_keys' => $cart_contains_item_key, 'cart_contains_ids' => $cart_contains); } else { $args['smart_offers'] = array('accept_offer' => true, 'offer_id' => $post_id, 'accepted_from' => $page); } } else { $args['smart_offers'] = array('accept_offer' => true, 'offer_id' => $post_id, 'accepted_from' => $page); } if (is_array($parent_offer_id) && count($parent_offer_id) > 0) { $args['smart_offers']['parent_offer_id'] = $parent_offer_id; } elseif (!is_array($parent_offer_id) && $parent_offer_id != '') { $args['smart_offers']['parent_offer_id'] = $parent_offer_id; } if (!empty($action_on_accept['add_to_cart']) && $action_on_accept['add_to_cart'] == 'yes') { $sa_smart_offers->global_wc()->cart->add_to_cart($parent_id, $quantity, $variation_id, $variation, $args); } $action_on_accept = get_post_meta($post_id, 'so_actions_on_accept', true); if (isset($action_on_accept['sa_apply_coupon']) && !empty($action_on_accept['sa_apply_coupon'])) { $coupons = explode(",", $action_on_accept['sa_apply_coupon']); if (is_array($coupons) && count($coupons) > 0) { foreach ($coupons as $coupon_title) { $sa_smart_offers->global_wc()->cart->add_discount($coupon_title); } } } if (isset($action_on_accept['sa_redirect_to_url']) && !empty($action_on_accept['sa_redirect_to_url'])) { $url = $action_on_accept['sa_redirect_to_url']; } elseif (isset($action_on_accept['accepted_offer_ids']) && !empty($action_on_accept['accepted_offer_ids'])) { $redirecting_option = explode(",", $action_on_accept['accepted_offer_ids']); if (is_array($redirecting_option) && count($redirecting_option) > 0) { foreach ($redirecting_option as $id) { $redirect_to = $id; } } ob_start(); if ($redirect_to != "") { $this->force_show_smart_offers($redirect_to); } } elseif (isset($action_on_accept['buy_now']) && $action_on_accept['buy_now'] == true && class_exists('WC_Buy_Now')) { $buy_now = new WC_Buy_Now(); $buy_now->checkout_redirect(); } else { if ($page == "cart_page") { $url = $sa_smart_offers->global_wc()->cart->get_cart_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(); } else { if ($page == "checkout_page" && $source == 'so_pre_checkout') { $url = $sa_smart_offers->global_wc()->cart->get_checkout_url(); } } } else { $url = $sa_smart_offers->global_wc()->cart->get_checkout_url(); } } if (empty($url)) { $url = wp_get_referer() ? wp_get_referer() : $sa_smart_offers->global_wc()->cart->get_cart_url(); } if (filter_var($url, FILTER_VALIDATE_URL) !== FALSE) { ob_clean(); wp_redirect($url); exit; } } }
/** * Empty SO related session data on logout */ function so_clear_session() { global $sa_smart_offers; SO_Session_Handler::so_delete_session('sa_smart_offers_skipped_offer_ids'); SO_Session_Handler::so_delete_session('sa_smart_offers_accepted_offer_ids'); $pages = array('cart', 'checkout', 'thankyou', 'myaccount', 'home', 'any'); foreach ($pages as $page) { SO_Session_Handler::so_delete_session($page . '_skip_offer_id'); SO_Session_Handler::so_delete_session($page . '_parent_offer_id'); } if ($sa_smart_offers->is_wc_gte_21()) { $data = !empty($sa_smart_offers->global_wc()->session) ? get_option('_wc_session_' . $sa_smart_offers->global_wc()->session->get_customer_id(), array()) : null; } else { $data = $_SESSION; } if (!empty($data)) { foreach ($data as $key_name => $value) { if (strpos($key_name, '_skip_offer_id') !== false || strpos($key_name, '_parent_offer_id') !== false) { SO_Session_Handler::so_delete_session($key_name); } } } }
/** * Return postmeta values of all offers of a particular page. */ function get_page_offers($page, $offer_ids = array()) { global $wpdb, $current_user; // ===== Calculating ids to skipped ======================= $offers_to_skip = array(); //Checking whehter session is set or not. $skipped_session_variable = SO_Session_Handler::check_session_set_or_not('sa_smart_offers_skipped_offer_ids'); $accepted_session_variable = SO_Session_Handler::check_session_set_or_not('sa_smart_offers_accepted_offer_ids'); // Getting skipped/accepted ids of session. $skipped_ids_in_session = $skipped_session_variable ? SO_Session_Handler::so_get_session_value('sa_smart_offers_skipped_offer_ids') : array(); $accepted_ids_in_session = $accepted_session_variable ? SO_Session_Handler::so_get_session_value('sa_smart_offers_accepted_offer_ids') : array(); if (!empty($skipped_ids_in_session)) { $offers_to_skip = array_merge($offers_to_skip, $skipped_ids_in_session); } if (!empty($accepted_session_variable)) { $offers_to_skip = array_merge($offers_to_skip, $accepted_ids_in_session); } if ($current_user->ID != 0) { $offers_skipped_by_user = get_user_meta($current_user->ID, 'customer_skipped_offers', true); if (!empty($offers_skipped_by_user)) { $offers_to_skip = array_merge($offers_to_skip, $offers_skipped_by_user); } } if (is_array($offers_to_skip)) { $offers_to_skip = array_unique($offers_to_skip, SORT_REGULAR); } // ======================================================= $results_for_fetching_offers = array(); $wpdb->query("SET SESSION group_concat_max_len=999999"); if (!empty($offer_ids)) { $smart_offers_ids_args = array('post_type' => 'smart_offers', 'post_status' => 'publish', 'post__in' => $offer_ids, 'nopaging' => true, 'fields' => 'ids'); } else { $smart_offers_ids_args = array('post_type' => 'smart_offers', 'post_status' => 'publish', 'nopaging' => true, 'fields' => 'ids', 'meta_query' => array(array('key' => 'offer_rule_page_options', 'value' => $page, 'compare' => 'LIKE'))); } if (!empty($offers_to_skip)) { $smart_offers_ids_args = array_merge($smart_offers_ids_args, array('post__not_in' => $offers_to_skip)); } $smart_offers_ids_result = new WP_Query($smart_offers_ids_args); $results_for_fetching_offers = $smart_offers_ids_result->post_count > 0 ? $smart_offers_ids_result->posts : array(); if (count($results_for_fetching_offers) > 0) { $offers = $this->get_all_offer_rules_meta($results_for_fetching_offers); } else { $offers = array(); } return $offers; }