0

I created module for set default zip code in checkout page by this answer - https://magento.stackexchange.com/a/293818/102895. It's work - the ZIP code is set when the page loads by this plugin.

But shipping methods not refresh. How can I set default zip code and refresh shipping methods in checkout page during load page?

6
  • please explain little bit more how you want to refresh shipping methods on pagelaod?
    – Mehran
    Commented May 18, 2023 at 13:13
  • I want the checkout page to load with the default zip code value that I set using the plugin, and that other services that require a zip code are loaded along with free standard delivery
    – WhoYou
    Commented May 18, 2023 at 13:21
  • try to add zipcode in shipping address when page loads it will automatically used for shipping method loads
    – Mehran
    Commented May 18, 2023 at 13:27
  • I'm new to Magento, how can I do this?
    – WhoYou
    Commented May 18, 2023 at 13:30
  • I tried set zipcode by this answer - magento.stackexchange.com/a/332491/102895 in JS, but isn't working for me
    – WhoYou
    Commented May 18, 2023 at 13:37

1 Answer 1

1

try this, Update LayoutProcessor.php

app\code\Ketan\Module\Plugin\Block\LayoutProcessor.php

<?php
namespace Ketan\Module\Plugin\Block;

class LayoutProcessor
{
    protected $checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->checkoutSession = $checkoutSession;
    }

    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        $postcode = 369856;
        $quote = $this->checkoutSession->getQuote();
        $shippingAddress = $quote->getShippingAddress();

        // Update the shipping address with the custom zip code
        $shippingAddress->setPostcode($postcode);

        $quote->save();
        /*================Start For Shipping Address ===========*/

        if(isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset'])) {

            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
            ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['postcode']['value'] = $postcode;

        }

        /*================End For Shipping Address ===========*/




        /*================Start For Billing Address ===========*/
        if(isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list'])) {
            $paymentsList = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list']['children'];

            foreach ($paymentsList as $paymentCode => $payment) {
                if(strpos($paymentCode, '-form') === false) {
                    continue;
                }
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$paymentCode]['children']['form-fields']['children']['postcode']['value'] = $postcode;
            }
        }
         if(isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children'])){


            $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['afterMethods']['children']['billing-address-form']['children']['form-fields']['children']['postcode']['value'] = $postcode;

        }

        /*================End For Billing Address ===========*/
        return $jsLayout;
    }

}
8
  • Checkout page loads with zip code, but I see only one shipping method - imgur.com/a/suiKa4u, but if I change zip code field manually - shipping methods refresh and a have two items
    – WhoYou
    Commented May 18, 2023 at 13:48
  • did you install any third party module?
    – Mehran
    Commented May 18, 2023 at 13:53
  • No, checkout module used from Magento. The initial task is to hide the free shipping method and activated another shipping method instead. I'm trying to do this by specifying the default zip code and then influencing the choice of delivery via JS. Maybe there is an easier way to hide the free shipping method when loading the default shipping page and choose another one, knowing its carrierCode and methodCode?
    – WhoYou
    Commented May 18, 2023 at 13:56
  • you want to hide freeshipping based on conditions? i am unable to understand what you are trying to achive?
    – Mehran
    Commented May 18, 2023 at 14:03
  • I want hide free shipping method during load checkout page and select other shipping method
    – WhoYou
    Commented May 18, 2023 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.