/** * Gets the price of shipping. * * @param array $package Order package. * * @return array Correios Quotes. */ protected function correios_calculate($package) { $services = array_values($this->correios_services()); $connect = new WC_Correios_Connect(); $connect->set_services($services); $_package = $connect->set_package($package); $_package->set_minimum_height($this->minimum_height); $_package->set_minimum_width($this->minimum_width); $_package->set_minimum_length($this->minimum_length); $connect->set_zip_origin($this->zip_origin); $connect->set_zip_destination($package['destination']['postcode']); $connect->set_debug($this->debug); if ('declare' == $this->declare_value) { $declared_value = $this->woocommerce_method()->cart->cart_contents_total; $connect->set_declared_value($declared_value); } if ('corporate' == $this->corporate_service) { $connect->set_login($this->login); $connect->set_password($this->password); } $shipping = $connect->get_shipping(); if (!empty($shipping)) { return $shipping; } else { // Cart only with virtual products. if ('yes' == $this->debug) { $this->log->add('correios', 'Cart only with virtual products.'); } return array(); } }
/** * Simulator ajax response. * * @return string */ public static function ajax_simulator() { // Validate the data. if (!isset($_GET['product_id']) || empty($_GET['product_id'])) { wp_send_json(array('error' => __('Error to identify the product.', 'woocommerce-correios'), 'rates' => '')); } if (!isset($_GET['zipcode']) || empty($_GET['zipcode'])) { wp_send_json(array('error' => __('Please enter with your zipcode.', 'woocommerce-correios'), 'rates' => '')); } // Get the product data. $id = isset($_GET['variation_id']) && !empty($_GET['variation_id']) ? $_GET['variation_id'] : $_GET['product_id']; $product_id = absint($id); $product = get_product($product_id); $quantity = isset($_GET['quantity']) && !empty($_GET['quantity']) ? $_GET['quantity'] : 1; // Test with the product exist. if (!$product) { wp_send_json(array('error' => __('Invalid product!', 'woocommerce-correios'), 'rates' => '')); } // Set the shipping params. $product_price = $product->get_price(); $zip_destination = $_GET['zipcode']; $options = get_option('woocommerce_correios_settings'); $minimum_height = isset($options['minimum_height']) ? $options['minimum_height'] : ''; $minimum_width = isset($options['minimum_width']) ? $options['minimum_width'] : ''; $minimum_length = isset($options['minimum_length']) ? $options['minimum_length'] : ''; $zip_origin = isset($options['zip_origin']) ? $options['zip_origin'] : ''; $display_date = isset($options['display_date']) ? $options['display_date'] : ''; $additional_time = isset($options['additional_time']) ? $options['additional_time'] : ''; $declare_value = isset($options['declare_value']) ? $options['declare_value'] : ''; $corporate_service = isset($options['corporate_service']) ? $options['corporate_service'] : ''; $login = isset($options['login']) ? $options['login'] : ''; $password = isset($options['password']) ? $options['password'] : ''; $fee = isset($options['fee']) ? $options['fee'] : ''; $debug = isset($options['debug']) ? $options['debug'] : ''; $package = array('contents' => array(array('data' => $product, 'quantity' => $quantity))); // Get the shipping. $services = array_values(self::get_correios_services($options)); $connect = new WC_Correios_Connect(); $connect->set_services($services); $_package = $connect->set_package($package); $_package->set_minimum_height($minimum_height); $_package->set_minimum_width($minimum_width); $_package->set_minimum_width($minimum_length); $connect->set_zip_origin($zip_origin); $connect->set_zip_destination($zip_destination); $connect->set_debug($debug); if ('declare' == $declare_value) { $connect->set_declared_value($product_price); } if ('corporate' == $corporate_service) { $connect->set_login($login); $connect->set_password($password); } $shipping = $connect->get_shipping(); // Send shipping rates. if (!empty($shipping)) { $_shipping = self::get_the_shipping($shipping, $options, $package); wp_send_json(array('error' => '', 'rates' => $_shipping)); } // Error. wp_send_json(array('error' => __('It was not possible to simulate the shipping, please try adding the product to cart and proceed to try to get the value.', 'woocommerce-correios'), 'rates' => '')); }