To archive this, you can fallback from your theme to Magento_Checkout/js/checkout-data.js
and change the function initData
to add your default postcode value, as you can see here:
/**
* @return {*}
*/
initData = function () {
return {
'selectedShippingAddress': null, //Selected shipping address pulled from persistence storage
'shippingAddressFromData': {
postcode: window.defaultCustomerZipCode
}, //Shipping address pulled from persistence storage
'newCustomerShippingAddress': null, //Shipping address pulled from persistence storage for customer
'selectedShippingRate': null, //Shipping rate pulled from persistence storage
'selectedPaymentMethod': null, //Payment method pulled from persistence storage
'selectedBillingAddress': null, //Selected billing address pulled from persistence storage
'billingAddressFromData': null, //Billing address pulled from persistence storage
'newCustomerBillingAddress': null //Billing address pulled from persistence storage for new customer
};
},
In this case, I'm taking the value from a custom block and putting there as a default value this way:
Create a layout file checkout_index_index.xml
in your module
<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block as="zipcode" class="Ziru\CustomerZipCode\Block\ZipCode" name="zipcode" template="Ziru_CustomerZipCode::zipcode.phtml"/>
</referenceContainer>
</body>
</page>
An then in the .phtml
file you can call your block and add the value to the window
object.
<?php
/**
* @var $block \Ziru\CustomerZipCode\Block\ZipCode
*/
?>
<div id="zip-code-details">
<script>
window.defaultCustomerZipCode = "<?= $block->getDefaultCustomerZipCode() ?>"
</script>
</div>
To take a way Magento will apply any value in
'shippingAddressFromData': {
postcode: window.defaultCustomerZipCode
},
So feel free to have your value there following any logic that you want. And also, Magento will save postcode
in the session as soon as the user changes this value. It means when the page gets refreshed the last value will be applied.
Lemme know if you could archive your challenger o/