function eStore_gateway_specific_buy_now_submit_listener()
{
    if (isset($_REQUEST['eStore_gs_buy_now_submit'])) {
        $wp_eStore_config = WP_eStore_Config::getInstance();
        $cookie_domain = eStore_get_top_level_domain();
        setcookie("cart_in_use", "true", time() + 21600, "/", $cookie_domain);
        if (function_exists('wp_cache_serve_cache_file')) {
            //WP Super cache workaround
            setcookie("comment_author_", "eStore", time() + 21600, "/", $cookie_domain);
        }
        unset($_SESSION['eStore_last_action_msg']);
        unset($_SESSION['eStore_last_action_msg_2']);
        unset($_SESSION['eStore_last_action_msg_3']);
        unset($_SESSION['eStore_gs_bn_co_error_msg']);
        if (isset($_SESSION['eStore_cart'])) {
            unset($_SESSION['eStore_cart']);
        }
        //sanitize data
        $_REQUEST['product'] = strip_tags($_REQUEST['product']);
        //for PHP5.2 use filter_var($_REQUEST['product'], FILTER_SANITIZE_STRING);
        $_REQUEST['add_qty'] = strip_tags($_REQUEST['add_qty']);
        $_REQUEST['item_number'] = strip_tags($_REQUEST['item_number']);
        if (isset($_REQUEST['custom_price'])) {
            $_REQUEST['custom_price'] = strip_tags($_REQUEST['custom_price']);
        }
        if (isset($_REQUEST['price'])) {
            $_REQUEST['price'] = strip_tags($_REQUEST['price']);
        }
        isset($_REQUEST['shipping']) ? $_REQUEST['shipping'] = strip_tags($_REQUEST['shipping']) : ($_REQUEST['shipping'] = '');
        isset($_REQUEST['cartLink']) ? $_REQUEST['cartLink'] = strip_tags($_REQUEST['cartLink']) : ($_REQUEST['cartLink'] = '');
        isset($_REQUEST['thumbnail_url']) ? $_REQUEST['thumbnail_url'] = strip_tags($_REQUEST['thumbnail_url']) : ($_REQUEST['thumbnail_url'] = '');
        isset($_REQUEST['tax']) ? $_REQUEST['tax'] = strip_tags($_REQUEST['tax']) : ($_REQUEST['tax'] = '');
        if (isset($_REQUEST['digital_flag'])) {
            $_REQUEST['digital_flag'] = strip_tags($_REQUEST['digital_flag']);
        } else {
            $_REQUEST['digital_flag'] = '';
        }
        $gateway = strip_tags($_REQUEST['eStore_gsbn_gateway']);
        $products = array();
        $eStore_gs_buy_now_checkout_error = false;
        $count = 1;
        if ($count == 1) {
            $item_addittion_permitted = true;
            $quantity_available = is_quantity_availabe($_REQUEST['item_number'], $_REQUEST['add_qty']);
            if (!$quantity_available) {
                //Requested qty not available
                $_REQUEST['add_qty'] = 1;
                //Add one by default
                $eStore_gs_buy_now_checkout_error = true;
            }
            if (isset($_SESSION['eStore_last_action_msg'])) {
                $_SESSION['eStore_gs_bn_co_error_msg'] = $_SESSION['eStore_last_action_msg'];
            }
            if ($item_addittion_permitted) {
                if (!empty($_REQUEST[$_REQUEST['product']])) {
                    $price = $_REQUEST[$_REQUEST['product']];
                } else {
                    if (isset($_REQUEST['custom_price'])) {
                        global $wpdb;
                        $products_table_name = WP_ESTORE_PRODUCTS_TABLE_NAME;
                        $id = $_REQUEST['item_number'];
                        $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$id}'", OBJECT);
                        if ($_REQUEST['custom_price'] < $retrieved_product->price) {
                            $price = $retrieved_product->price;
                            $currSymbol = get_option('cart_currency_symbol');
                            $eStore_gs_buy_now_checkout_error = true;
                            $_SESSION['eStore_gs_bn_co_error_msg'] = '<p style="color: red;">' . WP_ESTORE_MINIMUM_PRICE_YOU_CAN_ENTER . $currSymbol . $retrieved_product->price . '</p>';
                        } else {
                            $price = $_REQUEST['custom_price'];
                        }
                    } else {
                        $price = $_REQUEST['price'];
                    }
                }
                $product = array('name' => stripslashes($_REQUEST['product']), 'price' => $price, 'quantity' => $_REQUEST['add_qty'], 'shipping' => $_REQUEST['shipping'], 'item_number' => $_REQUEST['item_number'], 'cartLink' => $_REQUEST['cartLink'], 'thumbnail_url' => $_REQUEST['thumbnail_url'], 'tax' => $_REQUEST['tax'], 'digital_flag' => $_REQUEST['digital_flag']);
                array_push($products, $product);
            }
        }
        if (!$eStore_gs_buy_now_checkout_error) {
            $_SESSION['eStore_cart'] = $products;
            $_SESSION['eStore_url'] = WP_ESTORE_URL;
            $_SESSION['eStore_cart_sub_total'] = eStore_get_cart_total();
            $_SESSION['eStore_cart_postage_cost'] = eStore_get_cart_shipping();
            $_SESSION['eStore_cart_total_tax'] = eStore_calculate_total_cart_tax();
            wp_eStore_check_cookie_flag_and_store_values();
            if (WP_ESTORE_ENABLE_NEW_CHECKOUT_REDIRECTION === '1') {
                $redirect_page = WP_ESTORE_WP_SITE_URL . '/?eStore_checkout=process&eStore_gateway=' . $gateway;
            } else {
                $redirect_page = WP_ESTORE_URL . '/eStore_payment_submission.php?eStore_gateway=' . $gateway;
            }
            eStore_redirect_to_url($redirect_page);
        }
    }
}
Пример #2
0
function eStore_apply_discount($coupon)
{
    if (!isset($_SESSION['discount_applied_once']) || $_SESSION['discount_applied_once'] != 1 || WP_ESTORE_ALLOW_COUPON_STACKING === '1') {
        eStore_backup_estore_cart_before_coupon_application();
        $_SESSION['eStore_coupon_code'] = $coupon;
        global $wpdb;
        $coupon_table_name = $wpdb->prefix . "wp_eStore_coupon_tbl";
        $ret_coupon = $wpdb->get_row("SELECT * FROM {$coupon_table_name} WHERE coupon_code = '{$coupon}'", OBJECT);
        if ($ret_coupon) {
            $coupon_error = false;
            if ($ret_coupon->active != 'Yes') {
                $coupon_error = true;
                $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . ESTORE_COUPON_NOT_ACTIVE . '</p>';
            } else {
                if (!empty($ret_coupon->redemption_count) && $ret_coupon->redemption_count >= $ret_coupon->redemption_limit) {
                    $coupon_error = true;
                    $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . ESTORE_MAX_COUPON_USE . '</p>';
                }
            }
            //Check expiry
            if ($ret_coupon->expiry_date != '0000-00-00') {
                $todaysdate = strtotime(date("Y-m-d"));
                $expirydate = strtotime($ret_coupon->expiry_date);
                if ($expirydate < $todaysdate) {
                    $coupon_error = true;
                    $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . "Coupon code expired!" . '</p>';
                }
            }
            //Check start date
            if ($ret_coupon->start_date != '0000-00-00') {
                $todaysdate = strtotime(date("Y-m-d"));
                $startdate = strtotime($ret_coupon->start_date);
                if ($todaysdate < $startdate) {
                    $coupon_error = true;
                    $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . "This coupon code cannot be used until " . date('F j, Y', $startdate) . '</p>';
                }
            }
            if (!$coupon_error) {
                if (empty($ret_coupon->value)) {
                    $discount_amount = $ret_coupon->discount_value;
                    $discount_type = $ret_coupon->discount_type;
                    $discount_total = 0;
                    $products = $_SESSION['eStore_cart'];
                    if ($discount_type == 0) {
                        foreach ($products as $key => $item) {
                            if ($item['price'] > 0) {
                                $item_discount = $item['price'] * $discount_amount / 100;
                                $discount_total = $discount_total + $item_discount * $item['quantity'];
                                $item['price'] = $item['price'] - $item_discount;
                                unset($products[$key]);
                                array_push($products, $item);
                            }
                            $_SESSION['discount_applied_once'] = 1;
                        }
                    } else {
                        foreach ($products as $key => $item) {
                            if ($item['price'] - $discount_amount > 0) {
                                $discount_total = $discount_total + $discount_amount * $item['quantity'];
                                $item['price'] = $item['price'] - $discount_amount;
                            } else {
                                //Discount amount is bigger or same as item price
                                $discount_total = $discount_total + $item['price'];
                                $item['price'] = 0;
                            }
                            unset($products[$key]);
                            array_push($products, $item);
                            $_SESSION['discount_applied_once'] = 1;
                        }
                    }
                    $discount_total = round($discount_total, 2);
                    $discount_total = number_format($discount_total, 2, '.', '');
                    $_SESSION['eStore_discount_total'] = $discount_total;
                    $_SESSION['eStore_last_action_msg'] = '<p style="color: green;">' . ESTORE_TOTAL_DISCOUNT . WP_ESTORE_CURRENCY_SYMBOL . $discount_total . '</p>';
                    sort($products);
                    $_SESSION['eStore_cart'] = $products;
                } else {
                    $discount_total = round(eStore_apply_cond_discount($ret_coupon), 2);
                    if ($discount_total == -99) {
                        $_SESSION['discount_applied_once'] = 1;
                        //$_SESSION['eStore_discount_total'] = ESTORE_DISCOUNT_FREE_SHIPPING;
                        $_SESSION['eStore_last_action_msg'] = '<p style="color: green;">' . ESTORE_TOTAL_DISCOUNT . ESTORE_DISCOUNT_FREE_SHIPPING . '</p>';
                    } else {
                        if ($discount_total != 0) {
                            $discount_total = number_format($discount_total, 2, '.', '');
                            $_SESSION['eStore_discount_total'] = $discount_total;
                            $_SESSION['discount_applied_once'] = 1;
                            $_SESSION['eStore_last_action_msg'] = '<p style="color: green;">' . ESTORE_TOTAL_DISCOUNT . WP_ESTORE_CURRENCY_SYMBOL . $discount_total . '</p>';
                        } else {
                            $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . ESTORE_COUPON_COND_NOT_MET . '</p>';
                        }
                    }
                }
            }
            //end apply discount
        } else {
            $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . ESTORE_COUPON_INVALID . '</p>';
        }
    } else {
        $_SESSION['eStore_last_action_msg'] = '<p style="color: red;">' . ESTORE_DISCOUNT_LIMIT . '</p>';
    }
    $_SESSION['action_msg_set_time'] = time();
    do_action('eStore_action_cart_coupon_applied');
    wp_eStore_check_cookie_flag_and_store_values();
}