From e7542b39d3d1c20ce526227a505c1769dca1d77a Mon Sep 17 00:00:00 2001
From: Gundo Sifhufhi <sifhufhisg@gmail.com>
Date: Thu, 19 Jan 2023 16:29:47 +0200
Subject: [PATCH] Add Suburb Field. Send Store Origin `suburb field` with the
 Request Payload

---
 .idea/csv-editor.xml                          |  2 +-
 .idea/php.xml                                 |  5 +++
 Model/Carrier/Customshipping.php              | 11 ++---
 .../Checkout/Block/LayoutProcessorPlugin.php  | 41 +++++++++++++++++++
 etc/adminhtml/system.xml                      |  8 ++++
 etc/frontend/di.xml                           |  5 +++
 6 files changed, 66 insertions(+), 6 deletions(-)
 create mode 100644 Plugin/Checkout/Block/LayoutProcessorPlugin.php
 create mode 100644 etc/frontend/di.xml

diff --git a/.idea/csv-editor.xml b/.idea/csv-editor.xml
index 24b6446..d67b498 100644
--- a/.idea/csv-editor.xml
+++ b/.idea/csv-editor.xml
@@ -3,7 +3,7 @@
   <component name="CsvFileAttributes">
     <option name="attributeMap">
       <map>
-        <entry key="$PROJECT_DIR$/uafrica/Customshipping/i18n/en_US.csv">
+        <entry key="$PROJECT_DIR$/i18n/en_US.csv">
           <value>
             <Attribute>
               <option name="separator" value="," />
diff --git a/.idea/php.xml b/.idea/php.xml
index 0e09af4..0e1f041 100644
--- a/.idea/php.xml
+++ b/.idea/php.xml
@@ -1,5 +1,10 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="PhpIncludePathManager">
+    <include_path>
+      <path value="$PROJECT_DIR$/vendor/composer" />
+    </include_path>
+  </component>
   <component name="PhpProjectSharedConfiguration" php_language_level="7.4">
     <option name="suggestChangeDefaultLanguageLevel" value="false" />
   </component>
diff --git a/Model/Carrier/Customshipping.php b/Model/Carrier/Customshipping.php
index 306e2f0..c90a8ec 100644
--- a/Model/Carrier/Customshipping.php
+++ b/Model/Carrier/Customshipping.php
@@ -239,7 +239,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
 
         //Get all the origin data from the request
         /**  Origin Information  */
-        list($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier) = $this->storeInformation();
+        list($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb) = $this->storeInformation();
 
         $items = $request->getAllItems();
 
@@ -262,9 +262,11 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
                     'address1' => $originStreet1,
                     'address2' => $originStreet2,
                     'city' => $originCity,
+                    'suburb' => $originSuburb,
                     'province' => $originRegion,
                     'country_code' => $originCountry,
                     'postal_code' => $originStreet,
+
                 ],
                 'destination' => [
                     'company' => '', // TODO :: Add this if available
@@ -290,7 +292,6 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
     public function storeInformation(): array
     {
         /** Store Origin details */
-
         $originCountry = $this->_scopeConfig->getValue(
             'general/store_information/country_id',
             ScopeInterface::SCOPE_STORE
@@ -324,13 +325,13 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
             ScopeInterface::SCOPE_STORE
         );
 
-        $storePhoneNumber = $this->_scopeConfig->getValue(
-            'general/store_information/phone',
+        $originSuburb = $this->_scopeConfig->getValue(
+            'general/store_information/suburb',
             ScopeInterface::SCOPE_STORE
         );
 
         $baseIdentifier = $this->getBaseUrl();
-        return array($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier);
+        return array($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb);
     }
 
 
diff --git a/Plugin/Checkout/Block/LayoutProcessorPlugin.php b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
new file mode 100644
index 0000000..cc6ef51
--- /dev/null
+++ b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
@@ -0,0 +1,41 @@
+<?php
+namespace uafrica\Customshipping\Plugin\Checkout\Block;
+
+class LayoutProcessorPlugin
+{
+    /**
+     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
+     * @param array $jsLayout
+     * @return array
+     */
+    public function afterProcess(
+        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
+        array  $jsLayout
+    ) {
+
+        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
+        ['shippingAddress']['children']['shipping-address-fieldset']['children']['custom_field_text'] = [
+            'component' => 'Magento_Ui/js/form/element/abstract',
+            'config' => [
+                'customScope' => 'shippingAddress.custom_attributes',
+                'customEntry' => null,
+                'template' => 'ui/form/field',
+                'elementTmpl' => 'ui/form/element/input',
+                'options' => [],
+                'id' => 'suburb'
+            ],
+            'dataScope' => 'shippingAddress.custom_attributes.custom_field_text',
+            'label' => 'Suburb',
+            'provider' => 'checkoutProvider',
+            'visible' => true,
+            'validation' => [
+                'required-entry' => true
+            ],
+            'sortOrder' => 110,
+            /*'customEntry' => null,*/
+            'id' => 'suburb'
+        ];
+
+        return $jsLayout;
+    }
+}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index 9bbe52d..792020a 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -32,6 +32,14 @@
                 </field>
             </group>
         </section>
+        <section id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
+            <group id="store_information" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
+                <field id="suburb" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
+                    <label>Suburb</label>
+                    <can_be_empty>1</can_be_empty>
+                </field>
+            </group>
+        </section>
         <section id="carriers" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
             <group id="uafrica" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                 <label>uAfrica</label>
diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml
new file mode 100644
index 0000000..7a9bd2b
--- /dev/null
+++ b/etc/frontend/di.xml
@@ -0,0 +1,5 @@
+<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 name="add_custom_field_checkout_shipping_form" type="uafrica\Customshipping\Plugin\Checkout\Block\LayoutProcessorPlugin" sortOrder="10"/>
+    </type>
+</config>
-- 
GitLab