Advertisement
Guest User

ghfg

a guest
Sep 28th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. <?php
  2. class Webkul_Mpperproductshipping_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract
  3. {
  4. /* Use group alias */
  5. protected $_code = 'mpperproductshipping';
  6. public function collectRates(Mage_Shipping_Model_Rate_Request $request){
  7. //skip if not enabled
  8.  
  9. $postCode = $request->setDestPostcode();
  10. $restrictedCodes = array(1); //restricted values. they can come from anywhere
  11. if (in_array($postCode, $restrictedCodes)) {
  12. return false;
  13. }
  14.  
  15.  
  16. if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')||Mage::getStoreConfig('carriers/mp_multi_shipping/active')){
  17. return false;
  18. }
  19.  
  20. $result = Mage::getModel('shipping/rate_result');
  21. $session = Mage::getSingleton('checkout/session');
  22. $postcode=$session->getQuote()->getShippingAddress()->getPostcode();
  23. $countrycode=$session->getQuote()->getShippingAddress()->getCountry();
  24. $postcode=str_replace('-', '', $postcode);
  25. $shippingdetail=array();
  26. $shippostaldetail=array('countrycode'=>$countrycode,'postalcode'=>$postcode);
  27. /****/
  28. foreach($session->getQuote()->getAllVisibleItems() as $item) {
  29. $proid=$item->getProductId();
  30. $options=$item->getProductOptions();
  31. $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id'];
  32. if(!$mpassignproductId) {
  33. foreach($item->getOptions() as $option) {
  34. $temp=unserialize($option['value']);
  35. if($temp['mpassignproduct_id']) {
  36. $mpassignproductId=$temp['mpassignproduct_id'];
  37. }
  38. }
  39. }
  40. if($mpassignproductId) {
  41. $mpassignModel = Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId);
  42. $partner = $mpassignModel->getSellerId();
  43. } else {
  44. $collection=Mage::getModel('marketplace/product')
  45. ->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$proid));
  46. foreach($collection as $temp) {
  47. $partner=$temp->getUserid();
  48. }
  49. }
  50.  
  51. $product=Mage::getModel('catalog/product')->load($proid)->getWeight();
  52. $weight=$product*$item->getQty();
  53. if(count($shippingdetail)==0){
  54. array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId()));
  55. }else{
  56. $shipinfoflag=true;
  57. $index=0;
  58. foreach($shippingdetail as $itemship){
  59. if($itemship['seller_id']==$partner){
  60. $itemship['items_weight']=$itemship['items_weight']+$weight;
  61. $itemship['product_name']=$itemship['product_name'].",".$item->getName();
  62. $itemship['item_id']=$itemship['item_id'].",".$item->getId();
  63. $itemship['qty']=$itemship['qty']+$item->getQty();
  64. $shippingdetail[$index]=$itemship;
  65. $shipinfoflag=false;
  66. }
  67. $index++;
  68. }
  69. if($shipinfoflag==true){
  70. array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId()));
  71. }
  72. }
  73. }
  74. $shippingpricedetail=$this->getShippingPricedetail($shippingdetail,$shippostaldetail);
  75.  
  76. if($shippingpricedetail['errormsg']!==""){
  77. Mage::getSingleton('core/session')->setShippingCustomError($shippingpricedetail['errormsg']);
  78. return $result;
  79. }
  80. /*store shipping in session*/
  81. $shippingAll=Mage::getSingleton('core/session')->getData('shippinginfo');
  82. $shippingAll[$this->_code]=$shippingpricedetail['shippinginfo'];
  83. Mage::getSingleton('core/session')->setData('shippinginfo',$shippingAll);
  84.  
  85. $method = Mage::getModel('shipping/rate_result_method');
  86. $method->setCarrier($this->_code);
  87. $method->setCarrierTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/title'));
  88. /* Use method name */
  89. $method->setMethod($this->_code);
  90. $method->setMethodTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/name'));
  91. $method->setCost($shippingpricedetail['handlingfee']);
  92. $method->setPrice($shippingpricedetail['handlingfee']);
  93. $result->append($method);
  94. return $result;
  95. }
  96.  
  97. public function getShippingPricedetail($shippingdetail,$shippostaldetail) {
  98. $shippinginfo=array();
  99. $handling=0;
  100. $session = Mage::getSingleton('checkout/session');
  101. $customerAddress = $session->getQuote()->getShippingAddress();
  102. foreach($shippingdetail as $shipdetail) {
  103. $seller = Mage::getModel("customer/customer")->load($shipdetail['seller_id']);
  104. $sellerAddress = $seller->getPrimaryShippingAddress();
  105. $distance = $this->getDistanse($sellerAddress->getPostcode(),$customerAddress->getPostcode());
  106. $price = 0;
  107. $itemsarray=explode(',',$shipdetail['item_id']);
  108. foreach($session->getQuote()->getAllItems() as $item) {
  109. $proid=$item->getProductId();
  110. $options=$item->getProductOptions();
  111. $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id'];
  112. if(!$mpassignproductId) {
  113. foreach($item->getOptions() as $option) {
  114. $temp=unserialize($option['value']);
  115. if($temp['mpassignproduct_id']) {
  116. $mpassignproductId=$temp['mpassignproduct_id'];
  117. }
  118. }
  119. }
  120. $mpshippingcharge = 0;
  121. $localDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/local_shipping_distance');
  122. $regionalDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/regional_shipping_distance');
  123. $stateDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/state_shipping_distance');
  124. if(in_array($item->getId(),$itemsarray)) {
  125. if($mpassignproductId) {
  126. if($distance < $localDistance) {
  127. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getLocalShippingCharge();
  128. } elseif($distance > $localDistance && $distance < $regionalDistance) {
  129. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getRegionalShippingCharge();
  130. } elseif($distance > $regionalDistance) {
  131. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getStateShippingCharge();
  132. }
  133. } else {
  134. if($distance < $localDistance) {
  135. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpLocalShippingCharge();
  136. } elseif($distance > $localDistance && $distance < $regionalDistance) {
  137. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpRegionalShippingCharge();
  138. } elseif($distance > $regionalDistance) {
  139. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpStateShippingCharge();
  140. }
  141. }
  142. if(floatval($mpshippingcharge)==0){
  143. $price=$price+floatval($this->getConfigData('defalt_ship_amount'));
  144. }else{
  145. $price=$price+$mpshippingcharge;
  146. }
  147. }
  148. }
  149.  
  150. $handling = $handling+$price;
  151. $submethod = array(array('method'=>Mage::getStoreConfig('carriers/'.$this->_code.'/title'),'cost'=>$price,'error'=>0));
  152. array_push($shippinginfo,array('seller_id'=>$shipdetail['seller_id'],'methodcode'=>$this->_code,'shipping_ammount'=>$price,'product_name'=>$shipdetail['product_name'],'submethod'=>$submethod,'item_ids'=>$shipdetail['item_id']));
  153. }
  154. $msg="";
  155. return array('handlingfee'=>$handling,'shippinginfo'=>$shippinginfo,'errormsg'=>$msg);
  156. }
  157.  
  158. private function getDistanse($origin,$destination) {
  159. $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".$origin.",india&destinations=".$destination.",india&mode=driving&language=en-EN&sensor=false";
  160. $ch = curl_init();
  161. curl_setopt($ch, CURLOPT_URL, $url);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
  164. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  166. $response = curl_exec($ch);
  167. curl_close($ch);
  168. $response_all = json_decode($response);
  169. $distance = $response_all->rows[0]->elements[0]->distance->value / 1000;
  170. return $distance;
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement