Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function collectRates(Mage_Shipping_Model_Rate_Request $request)
- {
- if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
- return false;
- }
- $price = $this->getConfigData('price'); // set a default shipping price maybe 0
- $price = 0;
- //var_dump($request);
- // $api_url = "http://www.eshipper.com/fetch_rates";
- // $api_username = "opsapi";
- // $api_password = "opsapipass";
- $api_url = $this->getConfigData('api');
- $api_username = $this->getConfigData('apiusername');
- $api_password = $this->getConfigData('apipassword');
- $increase_rate = $this->getConfigData('increaserate');
- $weight_class = $this->getConfigData('weightclass');
- //Case1: Price Depends on Country,State and Pin Code
- $destCountry = $request->getDestCountryId();
- $destRegion = $request->getDestRegionId();
- $destRegionCode = $request->getDestRegionCode();
- // print_r($destStreet = $request->getDestStreet()); echo ': Dest Street<br/>';
- $destStreet = $request->getDestStreet();
- $destCity = $request->getDestCity();
- $destPostcode = $request->getDestPostcode();
- $country_id = $request->getCountryId();
- $region_id = $request->getRegionId();
- //$region_code = $request->getRegionCode();
- $city = $request->getCity();
- $postcode = $request->getPostcode();
- $region = Mage::getModel('directory/region')->load($region_id);
- $region_code = $region->getCode();
- $service_url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$destPostcode.'&sensor=false';
- $curl = curl_init($service_url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $curl_response = curl_exec($curl);
- if ($curl_response === false) {
- $info = curl_getinfo($curl);
- curl_close($curl);
- die('error occured during curl exec. Additioanl info: ' . var_export($info));
- }
- curl_close($curl);
- $decoded = json_decode($curl_response);
- if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
- die('error occured: ' . $decoded->response->errormessage);
- }
- $result = $decoded->results;
- $city_name = $result[0]->address_components[1]->long_name;
- if ($request->getAllItems()) {
- $total_item = count($request->getAllItems());
- $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
- $prod_html = '';
- $count_row = 1;
- foreach ($request->getAllItems() as $item) {
- if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
- continue;
- }
- if ($item->getHasChildren() && $item->isShipSeparately()) {
- $count_children = count($item->getHasChildren());
- $count_children_row = 1;
- foreach ($item->getChildren() as $child) {
- if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
- $product_id = $child->getProductId();
- $productObj = Mage::getModel('catalog/product')->load($product_id);
- $ship_price = $productObj->getData('shipping_price'); //our shipping attribute code
- //$price += (float)$ship_price;
- $prd_name = str_replace('"',"'",$productObj->getName());
- $prod_html .= '{"name":"'.$prd_name.'","sku":"'.$productObj->getSku().'","quantity":'.$item->getQty().',"weight":'.floor($item->getWeight()).',"weightUnit":"'.$weight_class.'","price":'.$item->getPrice().',"vendor":"null","requires_shipping":true,"taxable":true,"fulfillment_service":"manual","product_id":"'.$product_id.'","variant_id":410333945}';
- if($count_children_row<$count_children || $count_row<$total_item){
- $prod_html .= ',';
- }
- $count_children_row++;
- }
- }
- } else {
- $product_id = $item->getProductId();
- $productObj = Mage::getModel('catalog/product')->load($product_id);
- $prd_name = str_replace('"',"'",$productObj->getName());
- $prod_html .= '{"name":"'.$prd_name.'","sku":"'.$productObj->getSku().'","quantity":'.$item->getQty().',"weight":'.floor($item->getWeight()).',"weightUnit":"'.$weight_class.'","price":'.$item->getPrice().',"vendor":"null","requires_shipping":true,"taxable":true,"fulfillment_service":"manual","product_id":"'.$product_id.'","variant_id":410333945}';
- }
- if($count_row<$total_item){
- $prod_html .= ',';
- }
- $count_row++;
- }
- }
- //$fileContents = '{"rate":{"origin":{"country":"CA","postal_code":"V3N4R3","province":"ON","city":"Burnaby","name":null,"address1":"90 Maverick","address2":null,"address3":null,"phone":null,"fax":null,"address_type":null,"company_name":null},"destination":{"country":"'.$destCountry.'","postal_code":"'.$destPostcode.'","province":"'.$destRegionCode.'","city":"'.$destCity.'","name":null,"address1":null,"address2":"","address3":null,"phone":null,"fax":null,"address_type":null,"company_name":""},"items":['.$prod_html.'],"currency":"'.$currency.'"}}';
- $fileContents = '{"rate":{"origin":{"country":"'.$country_id.'","postal_code":"'.$postcode.'","province":"'.$region_code.'","city":"'.$city.'","name":null,"address1":null,"address2":null,"address3":null,"phone":null,"fax":null,"address_type":null,"company_name":null},"destination":{"country":"'.$destCountry.'","postal_code":"'.$destPostcode.'","province":"'.$destRegionCode.'","city":"'.$city_name.'","name":null,"address1":null,"address2":"","address3":null,"phone":null,"fax":null,"address_type":null,"company_name":""},"items":['.$prod_html.'],"currency":"'.$currency.'","CART_NAME":"MAGENTO"}}';
- //print_r($fileContents); exit;
- $ch = curl_init($api_url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'API_USERNAME: ' . $api_username,
- 'API_PASSWORD: ' . $api_password
- )
- );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContents);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = curl_exec($ch);
- //print_r($data);exit;
- if ($data !== FALSE) {
- $data = json_decode($data, TRUE);
- } else {
- $data = "";
- }
- $rate_array = array();
- if (is_array($data) && sizeof($data['rates']) > 0) {
- foreach ($data['rates'] as $key => $value) {
- $price = $value['total_price'] * 0.01;
- $price_eship_inc = $price + ($price * $increase_rate * 0.01);
- $max_delivery_date = "";
- if (!empty($value['max_delivery_date'])) {
- $max_delivery_date = " (Delivered by: " . $value['max_delivery_date'] . ")";
- } else {
- $max_delivery_date = "";
- }
- $np_label_shipping = $value['service_name'] . $max_delivery_date;
- if($price_eship_inc!=0)
- {
- $rate = array(
- 'id' => "eShipper_" . $value['service_code'],
- 'label' => $np_label_shipping,
- 'cost' => $price_eship_inc,
- 'calc_tax' => 'per_order'
- );
- // $this->add_rate($rate);
- $rate_array[] = $rate;
- //print_r($rate);
- }
- }
- } else {
- $rate = array(
- 'id' => "eShipper",
- 'label' => "Shipping Rates Not Available",
- 'cost' => 0.00,
- 'calc_tax' => 'per_order'
- );
- //$this->add_rate($rate);
- //print_r($rate);
- }
- $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
- $result = Mage::getModel('shipping/rate_result');
- $show = true;
- //print_r($rate_array);
- if($show){
- /*$method->setCarrierTitle($this->getConfigData('title'));
- $method->setMethodTitle($this->getConfigData('name'));
- $method->setPrice($price);
- $method->setCost($price);*/
- //echo $this->_code; exit;
- $i=0;
- foreach($rate_array as $value){
- $i++;
- $method = Mage::getModel('shipping/rate_result_method');
- $method->setCarrier($this->_code);
- $method->setMethod($this->_code.$i);
- $method->setCarrierTitle($this->getConfigData('title'));
- $method->setMethodTitle($value['label']);
- $method->setPrice($value['cost']);
- $method->setCost($value['cost']);
- $result->append($method);
- }
- }else{
- $error = Mage::getModel('shipping/rate_result_error');
- $error->setCarrier($this->_code);
- $error->setCarrierTitle($this->getConfigData('name'));
- $error->setErrorMessage($this->getConfigData('specificerrmsg'));
- $result->append($error);
- }
- return $result;
- }
- public function getAllowedMethods()
- {
- return array('excellence'=>$this->getConfigData('name'));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement