From 0140606eb9b29fe4bffe24bb6d8d4768c9cd1e2e Mon Sep 17 00:00:00 2001
From: Gundo Sifhufhi <sifhufhisg@gmail.com>
Date: Wed, 25 Jan 2023 09:31:02 +0200
Subject: [PATCH] Add Shipping Address `Suburb` Information. Added KnockoutJs
 mixins. Updated LayoutProcessor.

---
 Model/Carrier/Customshipping.php              |  9 ++++--
 Model/Carrier/uSub.php                        | 25 +++++++++++++++
 .../Checkout/Block/LayoutProcessorPlugin.php  |  6 ++--
 view/frontend/requirejs-config.js             |  9 ++++++
 .../action/set-shipping-information-mixin.js  | 32 +++++++++++++++++++
 5 files changed, 75 insertions(+), 6 deletions(-)
 create mode 100644 Model/Carrier/uSub.php
 create mode 100644 view/frontend/web/js/action/set-shipping-information-mixin.js

diff --git a/Model/Carrier/Customshipping.php b/Model/Carrier/Customshipping.php
index bf4b681..f0684dd 100644
--- a/Model/Carrier/Customshipping.php
+++ b/Model/Carrier/Customshipping.php
@@ -14,6 +14,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface;
 use Magento\Framework\Controller\Result\JsonFactory;
 use Magento\Framework\DataObject;
 use Magento\Framework\Exception\LocalizedException;
+use Magento\Framework\Exception\NoSuchEntityException;
 use Magento\Framework\HTTP\Client\CurlFactory;
 use Magento\Framework\Module\Dir\Reader;
 use Magento\Framework\Xml\Security;
@@ -235,7 +236,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
         $destCity = $request->getDestCity();
         $destStreet = $request->getDestStreet();
 
-        list($destStreet1, $destStreet2) = $this->destStreet($destStreet);
+        list($destStreet1, $destStreet2, $destStreet3) = $this->destStreet($destStreet);
 
         /**  Collection Origin Information  */
         list($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb) = $this->storeInformation();
@@ -271,7 +272,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
                     'company' => $destComp,
                     'address1' => $destStreet1,
                     'address2' => $destStreet2,
-                    'suburb' => '',
+                    'suburb' => $destStreet3,
                     'city' => $destCity,
                     'province' => $destRegion,
                     'country_code' => $destCountry,
@@ -872,10 +873,12 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
             $destStreet = explode("\n", $destStreet);
             $destStreet1 = $destStreet[0];
             $destStreet2 = $destStreet[1];
+            $destStreet3 = $destStreet[2] ?? '';
         } else {
             $destStreet1 = $destStreet;
             $destStreet2 = '';
+            $destStreet3 = '';
         }
-        return array($destStreet1, $destStreet2);
+        return array($destStreet1, $destStreet2, $destStreet3);
     }
 }
diff --git a/Model/Carrier/uSub.php b/Model/Carrier/uSub.php
new file mode 100644
index 0000000..38040f0
--- /dev/null
+++ b/Model/Carrier/uSub.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace uafrica\Customshipping\Model\Carrier;
+use Magento\Checkout\Api\Data\ShippingInformationInterface;
+use Magento\Framework\App\ObjectManager;
+use Magento\Framework\Exception\NoSuchEntityException;
+use Magento\Quote\Api\CartRepositoryInterface;
+
+
+class uSub {
+
+
+    /**
+     * @throws NoSuchEntityException
+     */
+    public function getDestSuburb()
+    {
+        $objectManager = ObjectManager::getInstance();
+        $quote = $objectManager->get('Magento\Checkout\Model\ShippingInformation')->getExtensionAttributes();
+        return $quote->getSuburb();
+    }
+
+
+
+}
diff --git a/Plugin/Checkout/Block/LayoutProcessorPlugin.php b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
index 183ce35..d379412 100644
--- a/Plugin/Checkout/Block/LayoutProcessorPlugin.php
+++ b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
@@ -35,11 +35,11 @@ class LayoutProcessorPlugin
                 'id' => 'suburb'
             ],
             'dataScope' => 'shippingAddress.extension_attributes.suburb',
-            'label' => 'Suburb',
+            'label' => 'Suburb(BobGo Required)',
             'provider' => 'checkoutProvider',
-            'visible' => true,
+            'visible' => false,
             'validation' => [
-                'required-entry' => true
+                'required-entry' => false
             ],
             'sortOrder' => 110,
             /*'customEntry' => null,*/
diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js
index e69de29..79d332f 100644
--- a/view/frontend/requirejs-config.js
+++ b/view/frontend/requirejs-config.js
@@ -0,0 +1,9 @@
+var config = {
+    config: {
+        mixins: {
+            'Magento_Checkout/js/action/set-shipping-information': {
+                'uafrica_Customshipping/js/action/set-shipping-information-mixin': true
+            }
+        }
+    }
+};
diff --git a/view/frontend/web/js/action/set-shipping-information-mixin.js b/view/frontend/web/js/action/set-shipping-information-mixin.js
new file mode 100644
index 0000000..1c8327b
--- /dev/null
+++ b/view/frontend/web/js/action/set-shipping-information-mixin.js
@@ -0,0 +1,32 @@
+/*jshint browser:true jquery:true*/
+/*global alert*/
+define([
+    'jquery',
+    'mage/utils/wrapper',
+    'Magento_Checkout/js/model/quote'
+], function ($, wrapper, quote) {
+    'use strict';
+
+    return function (setShippingInformationAction) {
+
+        return wrapper.wrap(setShippingInformationAction, function (originalAction) {
+            var shippingAddress = quote.shippingAddress();
+            if (shippingAddress['extension_attributes'] === undefined) {
+                shippingAddress['extension_attributes'] = {};
+            }
+
+            // var attribute = shippingAddress.customAttributes.find(
+            //     function (element) {
+            //         return element.attribute_code === 'suburb';
+            //     }
+            // );
+            //
+            // shippingAddress['extension_attributes']['suburb'] = attribute.value;
+
+       //     shippingAddress['extension_attributes']['suburb'] = shippingAddress.value;
+
+            // pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
+            return originalAction();
+        });
+    };
+});
-- 
GitLab