From b16053670eecb2fa4b28b10f6eda380e7c6e763c Mon Sep 17 00:00:00 2001
From: Gundo Sifhufhi <sifhufhisg@gmail.com>
Date: Tue, 14 Feb 2023 17:11:09 +0200
Subject: [PATCH] Added Custom attribute: Suburb field to RAC payload.

---
 Model/Carrier/CustomShipping.php              | 18 ++++-
 Model/Carrier/Suburb.php                      | 41 ++++++++++
 Model/Carrier/uSub.php                        | 20 ++---
 .../Checkout/Block/LayoutProcessorPlugin.php  | 79 ++++++-------------
 .../model/shipping-rates-validation-rules.js  |  3 +
 5 files changed, 92 insertions(+), 69 deletions(-)
 create mode 100644 Model/Carrier/Suburb.php

diff --git a/Model/Carrier/CustomShipping.php b/Model/Carrier/CustomShipping.php
index db29d6f..f2ae268 100644
--- a/Model/Carrier/CustomShipping.php
+++ b/Model/Carrier/CustomShipping.php
@@ -16,6 +16,7 @@ use Magento\Framework\DataObject;
 use Magento\Framework\Exception\LocalizedException;
 use Magento\Framework\HTTP\Client\CurlFactory;
 use Magento\Framework\Module\Dir\Reader;
+use Magento\Framework\View\Element\Template\Context;
 use Magento\Framework\Xml\Security;
 use Magento\Quote\Model\Quote\Address\RateRequest;
 use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
@@ -102,6 +103,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\
     protected JsonFactory $jsonFactory;
     private $cartRepository;
     private Company $company;
+    private uSub $newSub;
 
 
     /**
@@ -175,6 +177,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\
         );
         $this->jsonFactory = $jsonFactory;
         $this->curl = $curlFactory->create();
+        $this->newSub = new uSub();
         $this->company = new Company();
     }
 
@@ -250,6 +253,7 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\
             ];
         }
 
+        $destSubs = $this->getDestStreet();
         $payload = [
             'identifier' => $baseIdentifier,
             'rate' => [
@@ -268,7 +272,8 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\
                     'company' => $destComp,
                     'address1' => $destStreet1,
                     'address2' => $destStreet2,
-                    'suburb' => $destStreet3,
+                    'suburb' => $destSubs,//$destStreet3,
+
                     'city' => $destCity,
                     'province' => $destRegion,
                     'country_code' => $destCountry,
@@ -904,8 +909,17 @@ class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\
     /**
      * @return mixed|string
      */
-    protected function getDestComp(): mixed
+    public function getDestComp(): mixed
     {
         return $this->company->getDestComp();
     }
+
+    /*
+     * Get the suburb from the uSub class
+     * @return mixed|string
+     */
+    protected function getDestStreet(): mixed
+    {
+        return $this->newSub->getSuburb();
+    }
 }
diff --git a/Model/Carrier/Suburb.php b/Model/Carrier/Suburb.php
new file mode 100644
index 0000000..e8b18dd
--- /dev/null
+++ b/Model/Carrier/Suburb.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace bobgo\CustomShipping\Model\Carrier;
+
+
+use Magento\Checkout\Api\Data\ShippingInformationInterface;
+use Magento\Framework\View\Element\Template;
+use Magento\Framework\View\Element\Template\Context;
+
+class Suburb extends Template {
+
+    /**
+     * @var ShippingInformationInterface
+     */
+    private $_addressInformation;
+
+    /**
+     * @param Context $context
+     * @param ShippingInformationInterface $addressInformation
+     * @param array $data
+     */
+    public function __construct(
+        Context $context,
+        ShippingInformationInterface $addressInformation,
+        array $data = []
+    ) {
+        $this->_addressInformation = $addressInformation;
+        parent::__construct($context, $data);
+    }
+
+    /**
+     * Get custom Shipping Charge
+     *
+     * @return String
+     */
+    public function getShippingCharge()
+    {
+        $extAttributes = $this->_addressInformation->getExtensionAttributes();
+        return $extAttributes->getCustomField(); //get custom attribute data.
+    }
+}
diff --git a/Model/Carrier/uSub.php b/Model/Carrier/uSub.php
index 96ae474..87cb646 100644
--- a/Model/Carrier/uSub.php
+++ b/Model/Carrier/uSub.php
@@ -7,21 +7,13 @@ use Magento\Framework\Exception\NoSuchEntityException;
 use Magento\Quote\Api\CartRepositoryInterface;
 
 
-class uSub {
-
-
-    /**
-     * This is supposed to get the suburb from the shipping address extension attributes.
-     * Does not work for now, was supposed to access the suburb attribute from the shipping address extension attributes.
-     * @throws NoSuchEntityException
-     */
-    public function getDestSuburb()
+class uSub
+{
+    public function getSuburb(): mixed
     {
-        $objectManager = ObjectManager::getInstance();
-        $quote = $objectManager->get('Magento\Checkout\Model\ShippingInformation')->getExtensionAttributes();
-        return $quote->getSuburb();
+        //address.custom_attributes[0].value = "suburb value"
+        $data = json_decode(file_get_contents('php://input'), true);
+        return $data['address']['custom_attributes'][0]['value'] ?? '';
     }
 
-
-
 }
diff --git a/Plugin/Checkout/Block/LayoutProcessorPlugin.php b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
index 6eb5626..24a1328 100644
--- a/Plugin/Checkout/Block/LayoutProcessorPlugin.php
+++ b/Plugin/Checkout/Block/LayoutProcessorPlugin.php
@@ -22,61 +22,34 @@ class LayoutProcessorPlugin
         \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
         array  $jsLayout
     ) {
-        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
-        ['shippingAddress']['children']['shipping-address-fieldset']['children']['street'] = [
-            'component' => 'Magento_Ui/js/form/components/group',
-            'label' => ('Street Address'),
-            'required' => false,
-            'dataScope' => 'shippingAddress.street',
-            'provider' => 'checkoutProvider',
-            'sortOrder' => 55,
-            'type' => 'group',
-            'additionalClasses' => 'street',
-            'children' => [
-                [
-                    'label' => ('Street Address'),
-                    'component' => 'Magento_Ui/js/form/element/abstract',
-                    'config' => [
-                        'placeholder' => 'street address',
-                        'customScope' => 'shippingAddress',
-                        'template' => 'ui/form/field',
-                        'elementTmpl' => 'ui/form/element/input'
-                    ],
-                    'dataScope' => '0',
-                    'provider' => 'checkoutProvider',
-                    'validation' => ['required-entry' => true, "min_text_length" => 1, "max_text_length" => 255],
-                ],
-                [
-                    'label' => __('Label 2'),
-                    'component' => 'Magento_Ui/js/form/element/abstract',
-                    'config' => [
-                        'placeholder' => 'town',
-                        'customScope' => 'shippingAddress',
-                        'template' => 'ui/form/field',
-                        'elementTmpl' => 'ui/form/element/input'
-                    ],
-                    'dataScope' => '1',
-                    'provider' => 'checkoutProvider',
-                    'validation' => ['required-entry' => true, "min_text_length" => 1, "max_text_length" => 255],
-                ],
-                [
-                    'label' => __('Label 3'),
-                    'component' => 'Magento_Ui/js/form/element/abstract',
-                    'config' => [
-                        'placeholder' => 'suburb',
-                        'customScope' => 'shippingAddress',
-                        'template' => 'ui/form/field',
-                        'elementTmpl' => 'ui/form/element/input'
-                    ],
-                    'dataScope' => '2',
-                    'provider' => 'checkoutProvider',
-                    'validation' => ['required-entry' => true, "min_text_length" => 1, "max_text_length" => 255],
-                ],
-                    'dataScope' => '3',
-                    'provider' => 'checkoutProvider',
-                    'validation' => ['required-entry' => false, "min_text_length" => 1, "max_text_length" => 255],
+        $suburbAttribute = 'suburb';
+        $suburb = [
+            'component' => 'Magento_Ui/js/form/element/abstract',
+            'config' => [
+            'customScope' => 'shippingAddress.custom_attributes',
+                'customEntry' => null,
+                'template' => 'ui/form/field',
+                'elementTmpl' => 'ui/form/element/input',
+                'tooltip' => [
+                    'description' => 'Required for shipping accuracy',
                 ],
+            ],
+            'dataScope' => 'shippingAddress.custom_attributes' . '.' . $suburbAttribute,
+            'label' => 'Suburb',
+            'provider' => 'checkoutProvider',
+            'sortOrder' => 80,
+            'validation' => [
+                'required-entry' => true
+            ],
+            'options' => [],
+            'filterBy' => null,
+            'customEntry' => null,
+            'visible' => true,
+            'value' => '' // value field is used to set a default value of the attribute
         ];
+
+        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'][$suburbAttribute] = $suburb;
+
         return $jsLayout;
     }
 }
diff --git a/view/frontend/web/js/model/shipping-rates-validation-rules.js b/view/frontend/web/js/model/shipping-rates-validation-rules.js
index 0841e90..0cf80ca 100644
--- a/view/frontend/web/js/model/shipping-rates-validation-rules.js
+++ b/view/frontend/web/js/model/shipping-rates-validation-rules.js
@@ -18,6 +18,9 @@ define(
                     },
                     'city': {
                         'required': true
+                    },
+                    'suburb': {
+                        'required': true
                     }
                 };
             }
-- 
GitLab