0

I followed this link.

I created a custom shipping method and I need the city to be required for my shipping rates to be calculated.

My problem is my shipping-rates-validation doesn't get trigger it seems.

I checked from the Network by inspect element and my file swyft.js is not being called, only the default shipping methods (tablerate.js, flatrate.js) are being called. (see image below)

url

Also I get this message after checkout page is loaded - "No such entity with addressId = null" (see image below)

error message in checkout page

Below image is my custom city dropdown with validation and I wont get the shipping rates displayed:

city dropdown

The following is the code I implemented:

(Creating LayoutProcessorPlugin)

....\etc\di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin disabled="false" name="talliance_layout_checkout" sortOrder="11" type="Talliance\SwyftShippingMethod\Plugin\Magento\Layout\Model\Checkout\LayoutProcessorPlugin"/>
    </type>
</config>

....\Plugin\Magento\Layout\Model\Checkout

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;
}

....\view\frontend\layout\checkout_index_index.xml

<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>

....\view\frontend\web\js\model\shipping-rates-validation-rules\swyft.js

define(
    [],
    function () {
        'use strict';
        return {
            getRules: function() {
                return {
                    'city': {
                        'required': true
                    }
                };
            }
        };
    }
);

....\view\frontend\web\js\model\shipping-rates-validator\swyft.js

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;
        }
    };
}
);

....\view\frontend\web\js\view\shipping-rates-validation\swyft.js

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;
});

....\Model\Carrier\Swyft.php

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;
}

Please tell me what am i doing wrong? I can't figure out what is wrong. Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.