示例#1
0
    /**
     * General entry point for WPEC external shipping calculator
     * This function expects no arguments but requires POST data
     * and configuration from the plugin settings
     * @return array $rate_table List of rates in "Service"=>"Rate" format
     */
    function getQuote()
    {
        global $wpdb, $wpec_ash, $wpec_ash_tools, $wpsc_cart;
        $data = array();
        //************** These values are common to all entry points **************
        //*** User/Customer Entered Values ***\\
        //*** Set up the destination country ***\
        $data["dest_country"] = wpsc_get_customer_meta('shipping_country');
        $settings = get_option('wpec_usps');
        //Disable International Shipping. Default: Enabled as it currently is.
        $data['intl_rate'] = isset($settings['intl_rate']) && !empty($settings['intl_rate']) ? FALSE : TRUE;
        if (!$data['intl_rate'] && $data['dest_country'] != get_option('base_country')) {
            return array();
        }
        // If ths zip code is provided via a form post use it!
        $data["dest_zipcode"] = (string) wpsc_get_customer_meta('shippingpostcode');
        if (!is_object($wpec_ash_tools)) {
            $wpec_ash_tools = new ASHTools();
        }
        if (empty($data["dest_zipcode"]) && $wpec_ash_tools->needs_post_code($data["dest_country"])) {
            // We cannot get a quote without a zip code so might as well return!
            return array();
        }
        //*** Grab Total Weight from the shipment object for simple shipping
        $data["weight"] = wpsc_cart_weight_total();
        if (empty($data["weight"])) {
            return array();
        }
        // If the region code is provided via a form post use it!
        if (isset($_POST['region']) && !empty($_POST['region'])) {
            $data['dest_state'] = wpsc_get_region(sanitize_text_field($_POST['region']));
        } else {
            if ($dest_state = wpsc_get_customer_meta('shipping_state')) {
                // Well, we have a zip code in the session and no new one provided
                $data['dest_state'] = $dest_state;
            } else {
                $data['dest_state'] = "";
            }
        }
        $data["dest_country"] = $wpec_ash_tools->get_full_country($data["dest_country"]);
        $data["dest_country"] = $this->_update_country($data["dest_country"]);
        if (!is_object($wpec_ash)) {
            $wpec_ash = new ASH();
        }
        $shipping_cache_check['state'] = $data['dest_state'];
        $shipping_cache_check['country'] = $data['dest_country'];
        $shipping_cache_check['zipcode'] = $data["dest_zipcode"];
        $this->shipment = $wpec_ash->get_shipment();
        $this->shipment->set_destination($this->internal_name, $shipping_cache_check);
        $this->shipment->rates_expire = date('Y-m-d');
        //Date will be checked against the cached date.
        $data['shipper'] = $this->internal_name;
        $data["adv_rate"] = !empty($settings["adv_rate"]) ? $settings["adv_rate"] : FALSE;
        // Use advanced shipping for Domestic Rates ? Not available
        if ($data["weight"] > 70 && !(bool) $data["adv_rate"]) {
            //USPS has a weight limit: https://www.usps.com/send/can-you-mail-it.htm?#3.
            $over_weight_txt = apply_filters('wpsc_shipment_over_weight', __('Your order exceeds the standard shipping weight limit.
													Please contact us to quote other shipping alternatives.', 'wp-e-commerce'), $data);
            $shipping_quotes[$over_weight_txt] = 0;
            // yes, a constant.
            $wpec_ash->cache_results($this->internal_name, array($shipping_quotes), $this->shipment);
            return array($shipping_quotes);
        }
        // Check to see if the cached shipment is still accurate, if not we need new rate
        $cache = $wpec_ash->check_cache($this->internal_name, $this->shipment);
        // We do not want to spam USPS (and slow down our process) if we already
        // have a shipping quote!
        if (count($cache["rate_table"]) >= 1) {
            //$cache['rate_table'] could be array(0).
            return $cache["rate_table"];
        }
        //*** WPEC Configuration values ***\\
        $this->use_test_env = !isset($settings["test_server"]) ? false : (bool) $settings['test_server'];
        $data["fcl_type"] = !empty($settings["fcl_type"]) ? $settings["fcl_type"] : "PARCEL";
        $data["mail_type"] = !empty($settings["intl_pkg"]) ? $settings["intl_pkg"] : "Package";
        $data["base_zipcode"] = get_option("base_zipcode");
        $data["services"] = !empty($settings["services"]) ? $settings["services"] : array("STANDARD POST", "PRIORITY", "PRIORITY EXPRESS", "FIRST CLASS");
        foreach ($data["services"] as $id => $service) {
            if ($service == 'PARCEL') {
                $data["services"][$id] = 'STANDARD POST';
            }
            if ($service == 'EXPRESS') {
                $data["services"][$id] = 'PRIORITY EXPRESS';
            }
        }
        $data["user_id"] = $settings["id"];
        $data["value"] = $wpsc_cart->calculate_subtotal(true);
        //Required by $this->_build_intl_shipment.
        $data = apply_filters('wpsc_shipment_data', $data, $this->shipment);
        if (isset($data['stop'])) {
            //Do not get rates.
            return array();
        }
        //************ GET THE RATE ************\\
        $rate_table = apply_filters('wpsc_rates_table', $this->_run_quote($data), $data, $this->shipment);
        //Avoid trying getting rates again and again when the stored zip code is incorrect.
        //************ CACHE the Results ************\\
        $wpec_ash->cache_results($this->internal_name, $rate_table, $this->shipment);
        return $rate_table;
    }