Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <type name="MagentoCheckoutBlockCheckoutLayoutProcessor">
- <plugin disabled="false" name="talliance_layout_checkout" sortOrder="11" type="TallianceSwyftShippingMethodPluginMagentoLayoutModelCheckoutLayoutProcessorPlugin"/>
- </type>
- </config>
- public function afterProcess(LayoutProcessor $layoutProcessor, array $jsLayout)
- {
- $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
- ['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = [
- 'component' => 'Magento_Ui/js/form/element/select',
- 'config' => [
- 'customScope' => 'shippingAddress',
- 'template' => 'ui/form/field',
- 'elementTmpl' => 'ui/form/element/select',
- 'id' => 'city'
- ],
- 'dataScope' => 'shippingAddress.city',
- 'label' => 'City',
- 'provider' => 'checkoutProvider',
- 'visible' => true,
- 'validation' => [ 'required-entry' => true ],
- 'sortOrder' => 300,
- 'id' => 'city',
- 'options' => $this->_cities->toOptionArray(),
- ];
- return $jsLayout;
- }
- <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
- <body>
- <referenceBlock name="checkout.root">
- <arguments>
- <argument name="jsLayout" xsi:type="array">
- <item name="components" xsi:type="array">
- <item name="checkout" xsi:type="array">
- <item name="children" xsi:type="array">
- <item name="steps" xsi:type="array">
- <item name="children" xsi:type="array">
- <item name="shipping-step" xsi:type="array">
- <item name="children" xsi:type="array">
- <item name="step-config" xsi:type="array">
- <item name="children" xsi:type="array">
- <item name="shipping-rates-validation" xsi:type="array">
- <item name="children" xsi:type="array">
- <item name="swyft-rates-validation" xsi:type="array">
- <item name="component" xsi:type="string">Talliance_SwyftShippingMethod/js/view/shipping-rates-validation/swyft</item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </item>
- </argument>
- </arguments>
- </referenceBlock>
- </body>
- </page>
- define(
- [],
- function () {
- 'use strict';
- return {
- getRules: function() {
- return {
- 'city': {
- 'required': true
- }
- };
- }
- };
- }
- );
- define(
- [
- 'jquery',
- 'mageUtils',
- '../shipping-rates-validation-rules/swyft',
- 'mage/translate'
- ],
- function ($, utils, validationRules, $t) {
- 'use strict';
- return {
- validationErrors: [],
- validate: function(address) {
- var self = this;
- this.validationErrors = [];
- $.each(validationRules.getRules(), function (field, rule) {
- var message;
- if (rule.required && utils.isEmpty(address[field])) {
- message = $t('Field ') + field + $t(' is required.');
- self.validationErrors.push(message);
- }
- });
- return !this.validationErrors.length;
- }
- };
- }
- );
- define(
- [
- 'uiComponent',
- 'Magento_Checkout/js/model/shipping-rates-validator',
- 'Magento_Checkout/js/model/shipping-rates-validation-rules',
- '../../model/shipping-rates-validator/swyft',
- '../../model/shipping-rates-validation-rules/swyft'
- ],
- function (
- Component,
- defaultShippingRatesValidator,
- defaultShippingRatesValidationRules,
- swyftShippingRatesValidator,
- swyftShippingRatesValidationRules
- ) {
- 'use strict';
- defaultShippingRatesValidator.registerValidator('swyft', swyftShippingRatesValidator);
- defaultShippingRatesValidationRules.registerRules('swyft', swyftShippingRatesValidationRules);
- return Component;
- });
- public function collectRates(RateRequest $request)
- {
- if (!$this->getConfigFlag('active')) {
- return false;
- }
- // $data = json_decode(file_get_contents('php://input'), true);
- // $city = $data['address']['shippingAddress']['city'];
- // $this->_logger->log(100, print_r($city,true));
- $rate = $this->getShippingRates();
- $this->_logger->log(100, print_r('rate cost: '.$rate,true));
- if (!$rate) {
- return false;
- }
- $result = $this->_rateFactory->create();
- $method = $this->_rateMethodFactory->create();
- $method->setCarrier($this->_code);
- $method->setCarrierTitle($this->getConfigData('title'));
- $method->setMethod($this->_code);
- $method->setMethodTitle($this->getConfigData('name'));
- $method->setCost($rate);
- $method->setPrice($rate);
- $result->append($method);
- return $result;
- }
Add Comment
Please, Sign In to add comment