Guest User

Untitled

a guest
Jan 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:n*emphasized text*oNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3. <type name="VendorModuleModelRateResult">
  4. <plugin name="vendor_shippingrules_update_rate_result"
  5. type="VendorModuleModelPluginShippingRateResultAppend"
  6. sortOrder="10"
  7. disabled="false"/>
  8. <plugin name="vendor_shippingrules_update_disabled_or_enabled_rates"
  9. type="VendorModuleModelPluginShippingRateResultGetAllRates"
  10. sortOrder="11"
  11. disabled="false"/>
  12. </type>
  13. </config>
  14.  
  15. /**
  16. * Copyright © 2016 MageWorx. All rights reserved.
  17. * See LICENSE.txt for license details.
  18. */
  19.  
  20. namespace VendorModuleModelPluginShippingRateResult;
  21.  
  22. class GetAllRates
  23. {
  24.  
  25. /**
  26. * Disable the marked shipping rates.
  27. *
  28. * NOTE: If you can not see some of the shipping rates, start debugging from here. At first, check 'is_disabled'
  29. * param in the shipping rate object.
  30. *
  31. * @param MagentoShippingModelRateResult $subject
  32. * @param array $result
  33. * @return array
  34. */
  35. public function afterGetAllRates($subject, $result)
  36. {
  37. foreach ($result as $key => $rate) {
  38. if ($rate->getIsDisabled()) {
  39. unset($result[$key]);
  40. }
  41. }
  42.  
  43. return $result;
  44. }
  45. }
  46.  
  47. namespace VendorModuleModelPluginShippingRateResult;
  48.  
  49. class Append
  50. {
  51. /**
  52. * @var MagentoCheckoutModelSession|MagentoBackendModelSessionQuote
  53. */
  54. protected $session;
  55.  
  56. /**
  57. * @param MagentoCheckoutModelSession $checkoutSession
  58. * @param MagentoBackendModelSessionQuote $backendQuoteSession
  59. * @param MagentoFrameworkAppState $state
  60. * @internal param Session $session
  61. */
  62. public function __construct(
  63. MagentoCheckoutModelSession $checkoutSession,
  64. MagentoBackendModelSessionQuote $backendQuoteSession,
  65. MagentoFrameworkAppState $state
  66. ) {
  67. if ($state->getAreaCode() == MagentoFrameworkAppArea::AREA_ADMINHTML) {
  68. $this->session = $backendQuoteSession;
  69. } else {
  70. $this->session = $checkoutSession;
  71. }
  72. }
  73.  
  74. /**
  75. * Validate each shipping method before append.
  76. * Apply the rules action if validation was successful.
  77. * Can mark some rules as disabled. The disabled rules will be removed in the class
  78. * @see MageWorxShippingRulesModelPluginShippingRateResultGetAllRates
  79. * by checking the value of this mark in the rate object.
  80. *
  81. * NOTE: If you have some problems with the rules and the shipping methods, start debugging from here.
  82. *
  83. * @param MagentoShippingModelRateResult $subject
  84. * @param MagentoQuoteModelQuoteAddressRateResultAbstractResult|MagentoShippingModelRateResult $result
  85. * @return array
  86. */
  87. public function beforeAppend($subject, $result)
  88. {
  89. if (!$result instanceof MagentoQuoteModelQuoteAddressRateResultMethod) {
  90. return [$result];
  91. }
  92.  
  93. $filtableMethods = [
  94. 'flatrate_flatrate',
  95. 'ups_XDM',
  96. 'ups_XPR',
  97. 'ups_WXS',
  98. 'carrier_method',
  99. // ... add here your method codes
  100. ];
  101. $methodCode = $result->getCarrier() . '_' . $result->getMethod();
  102. if (!in_array($methodCode, $filtableMethods)) {
  103. return [$result];
  104. }
  105.  
  106. /** @var MagentoQuoteModelQuote $quote */
  107. $quote = $this->session->getQuote();
  108. $quoteItems = $quote->getAllItems();
  109. $heavyWeightFlag = false;
  110. foreach ($quoteItems as $item) {
  111. if ($item->getWeight() > 100) {
  112. $heavyWeightFlag = true;
  113. continue;
  114. }
  115. }
  116.  
  117. if ($heavyWeightFlag == true) {
  118. $result->setIsDisabled(true);
  119. }
  120.  
  121. return [$result];
  122. }
  123. }
Add Comment
Please, Sign In to add comment