Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:n*emphasized text*oNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="VendorModuleModelRateResult">
- <plugin name="vendor_shippingrules_update_rate_result"
- type="VendorModuleModelPluginShippingRateResultAppend"
- sortOrder="10"
- disabled="false"/>
- <plugin name="vendor_shippingrules_update_disabled_or_enabled_rates"
- type="VendorModuleModelPluginShippingRateResultGetAllRates"
- sortOrder="11"
- disabled="false"/>
- </type>
- </config>
- /**
- * Copyright © 2016 MageWorx. All rights reserved.
- * See LICENSE.txt for license details.
- */
- namespace VendorModuleModelPluginShippingRateResult;
- class GetAllRates
- {
- /**
- * Disable the marked shipping rates.
- *
- * NOTE: If you can not see some of the shipping rates, start debugging from here. At first, check 'is_disabled'
- * param in the shipping rate object.
- *
- * @param MagentoShippingModelRateResult $subject
- * @param array $result
- * @return array
- */
- public function afterGetAllRates($subject, $result)
- {
- foreach ($result as $key => $rate) {
- if ($rate->getIsDisabled()) {
- unset($result[$key]);
- }
- }
- return $result;
- }
- }
- namespace VendorModuleModelPluginShippingRateResult;
- class Append
- {
- /**
- * @var MagentoCheckoutModelSession|MagentoBackendModelSessionQuote
- */
- protected $session;
- /**
- * @param MagentoCheckoutModelSession $checkoutSession
- * @param MagentoBackendModelSessionQuote $backendQuoteSession
- * @param MagentoFrameworkAppState $state
- * @internal param Session $session
- */
- public function __construct(
- MagentoCheckoutModelSession $checkoutSession,
- MagentoBackendModelSessionQuote $backendQuoteSession,
- MagentoFrameworkAppState $state
- ) {
- if ($state->getAreaCode() == MagentoFrameworkAppArea::AREA_ADMINHTML) {
- $this->session = $backendQuoteSession;
- } else {
- $this->session = $checkoutSession;
- }
- }
- /**
- * Validate each shipping method before append.
- * Apply the rules action if validation was successful.
- * Can mark some rules as disabled. The disabled rules will be removed in the class
- * @see MageWorxShippingRulesModelPluginShippingRateResultGetAllRates
- * by checking the value of this mark in the rate object.
- *
- * NOTE: If you have some problems with the rules and the shipping methods, start debugging from here.
- *
- * @param MagentoShippingModelRateResult $subject
- * @param MagentoQuoteModelQuoteAddressRateResultAbstractResult|MagentoShippingModelRateResult $result
- * @return array
- */
- public function beforeAppend($subject, $result)
- {
- if (!$result instanceof MagentoQuoteModelQuoteAddressRateResultMethod) {
- return [$result];
- }
- $filtableMethods = [
- 'flatrate_flatrate',
- 'ups_XDM',
- 'ups_XPR',
- 'ups_WXS',
- 'carrier_method',
- // ... add here your method codes
- ];
- $methodCode = $result->getCarrier() . '_' . $result->getMethod();
- if (!in_array($methodCode, $filtableMethods)) {
- return [$result];
- }
- /** @var MagentoQuoteModelQuote $quote */
- $quote = $this->session->getQuote();
- $quoteItems = $quote->getAllItems();
- $heavyWeightFlag = false;
- foreach ($quoteItems as $item) {
- if ($item->getWeight() > 100) {
- $heavyWeightFlag = true;
- continue;
- }
- }
- if ($heavyWeightFlag == true) {
- $result->setIsDisabled(true);
- }
- return [$result];
- }
- }
Add Comment
Please, Sign In to add comment