public function add_subscription_args($args, $offer_id, $action)
 {
     if (empty($args) || $action == 'skip') {
         return $args;
     }
     $so_offers = new SO_Offers();
     $user_details = $so_offers->get_user_details();
     $user_has_bought = !empty($user_details['offer_rule_has_bought']) ? explode(',', $user_details['offer_rule_has_bought']) : array();
     $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions();
     $preserve_keys = true;
     $subscriptions = array_reverse($subscriptions, $preserve_keys);
     foreach ($subscriptions as $subscription_key => $subscription) {
         if (in_array($subscription['product_id'], $user_has_bought) && 'active' == $subscription['status']) {
             $args['switch-subscription'] = $subscription_key;
             $args['auto-switch'] = 'true';
             break;
         }
     }
     return $args;
 }
예제 #2
0
 function is_offer_valid($page, $offer_ids)
 {
     global $sa_smart_offers;
     $so_offers = new SO_Offers();
     //Get user's details
     $user_details = $so_offers->get_user_details($page, '');
     $dp = (int) get_option('woocommerce_price_num_decimals');
     $sa_smart_offers->global_wc()->cart->calculate_shipping();
     if ($sa_smart_offers->is_wc_gte_20()) {
         if ($sa_smart_offers->is_wc_gte_23()) {
             $cart_total = apply_filters('woocommerce_calculated_total', round($sa_smart_offers->global_wc()->cart->cart_contents_total + $sa_smart_offers->global_wc()->cart->tax_total + $sa_smart_offers->global_wc()->cart->shipping_tax_total + $sa_smart_offers->global_wc()->cart->shipping_total + $sa_smart_offers->global_wc()->cart->fee_total, $sa_smart_offers->global_wc()->cart->dp), $sa_smart_offers->global_wc()->cart);
         } else {
             $cart_total = apply_filters('woocommerce_calculated_total', number_format($sa_smart_offers->global_wc()->cart->cart_contents_total + $sa_smart_offers->global_wc()->cart->tax_total + $sa_smart_offers->global_wc()->cart->shipping_tax_total + $sa_smart_offers->global_wc()->cart->shipping_total - $sa_smart_offers->global_wc()->cart->discount_total + $sa_smart_offers->global_wc()->cart->fee_total, $dp, '.', ''), $sa_smart_offers->global_wc()->cart);
         }
     } else {
         $cart_total = apply_filters('woocommerce_calculated_total', number_format($sa_smart_offers->global_wc()->cart->cart_contents_total + $sa_smart_offers->global_wc()->cart->tax_total + $sa_smart_offers->global_wc()->cart->shipping_tax_total + $sa_smart_offers->global_wc()->cart->shipping_total - $sa_smart_offers->global_wc()->cart->discount_total, $dp, '.', ''), $sa_smart_offers->global_wc()->cart);
     }
     // Get Cart/Order details
     $cart_order_details = $so_offers->get_cart_contents();
     $cart_order_details['offer_rule_grand_total'] = $cart_total;
     $details = array_merge($user_details, $cart_order_details);
     $offer_to_validate = $so_offers->get_all_offer_rules_meta($offer_ids);
     $valid_offer_ids = $so_offers->validate_offers($page, $offer_to_validate, $details);
     $key = !empty($offer_ids[0]) ? $offer_ids[0] : 0;
     if (!empty($valid_offer_ids) && array_key_exists($key, $valid_offer_ids)) {
         return $valid_offer_ids;
     } else {
         return false;
     }
 }
예제 #3
0
 /**
  * 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;
         }
     }
 }
예제 #4
0
 /**
  * Shortcode to show offer
  */
 function shortcode_for_showing_offers($atts)
 {
     extract(shortcode_atts(array('display_as' => '', 'offer_ids' => ''), $atts));
     $so_offers = new SO_Offers();
     $offers_data = $so_offers->get_offers($offer_ids);
     if (empty($offers_data)) {
         return;
     }
     $so_offer = new SO_Offer();
     $so_offer->prepare_offer($display_as, $offers_data);
 }