Guest User

Untitled

a guest
Apr 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. public function collectRates(RateRequest $request)
  2. {
  3. if (!$this->getConfigFlag('active')) {
  4. return false;
  5. }
  6.  
  7. // exclude Virtual products price from Package value if pre-configured
  8. if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) {
  9. foreach ($request->getAllItems() as $item) {
  10. if ($item->getParentItem()) {
  11. continue;
  12. }
  13. if ($item->getHasChildren() && $item->isShipSeparately()) {
  14. foreach ($item->getChildren() as $child) {
  15. if ($child->getProduct()->isVirtual()) {
  16. $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal());
  17. }
  18. }
  19. } elseif ($item->getProduct()->isVirtual()) {
  20. $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal());
  21. }
  22. }
  23. }
  24.  
  25. // Free shipping by qty
  26. $freeQty = 0;
  27. if ($request->getAllItems()) {
  28. $freePackageValue = 0;
  29. foreach ($request->getAllItems() as $item) {
  30. if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
  31. continue;
  32. }
  33.  
  34. if ($item->getHasChildren() && $item->isShipSeparately()) {
  35. foreach ($item->getChildren() as $child) {
  36. if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
  37. $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0;
  38. $freeQty += $item->getQty() * ($child->getQty() - $freeShipping);
  39. }
  40. }
  41. } elseif ($item->getFreeShipping()) {
  42. $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0;
  43. $freeQty += $item->getQty() - $freeShipping;
  44. $freePackageValue += $item->getBaseRowTotal();
  45. }
  46. }
  47. $oldValue = $request->getPackageValue();
  48. $request->setPackageValue($oldValue - $freePackageValue);
  49. }
  50.  
  51. if (!$request->getConditionName()) {
  52. $conditionName = $this->getConfigData('condition_name');
  53. $request->setConditionName($conditionName ? $conditionName : $this->_defaultConditionName);
  54. }
  55.  
  56. // Package weight and qty free shipping
  57. $oldWeight = $request->getPackageWeight();
  58. $oldQty = $request->getPackageQty();
  59.  
  60. $request->setPackageWeight($request->getFreeMethodWeight());
  61. $request->setPackageQty($oldQty - $freeQty);
  62.  
  63. /** @var MagentoShippingModelRateResult $result */
  64. $result = $this->_rateResultFactory->create();
  65. $rate = $this->getRate($request);
  66.  
  67. $request->setPackageWeight($oldWeight);
  68. $request->setPackageQty($oldQty);
  69.  
  70. if (!empty($rate) && $rate['price'] >= 0) {
  71. /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
  72. $method = $this->_resultMethodFactory->create();
  73.  
  74. $method->setCarrier('tablerate');
  75. $method->setCarrierTitle($this->getConfigData('title'));
  76.  
  77. $method->setMethod('bestway');
  78. $method->setMethodTitle($this->getConfigData('name'));
  79.  
  80. if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
  81. $shippingPrice = 0;
  82. } else {
  83. $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
  84. }
  85.  
  86. $method->setPrice($shippingPrice);
  87. $method->setCost($rate['cost']);
  88.  
  89. $result->append($method);
  90. } else {
  91. /** @var MagentoQuoteModelQuoteAddressRateResultError $error */
  92. $error = $this->_rateErrorFactory->create(
  93. [
  94. 'data' => [
  95. 'carrier' => $this->_code,
  96. 'carrier_title' => $this->getConfigData('title'),
  97. 'error_message' => $this->getConfigData('specificerrmsg'),
  98. ],
  99. ]
  100. );
  101. $result->append($error);
  102. }
  103.  
  104. return $result;
  105.  
  106. }
Add Comment
Please, Sign In to add comment