diff --git a/Model/Carrier/CustomShipping.php b/Model/Carrier/CustomShipping.php index db29d6f28055da6bc174794270d0091ede229ee2..f2ae2682c08008894481c20610950770fd11022c 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 0000000000000000000000000000000000000000..e8b18dd63d2c00a80a91e451ff68f431ae29c039 --- /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 96ae4747c1b928eefd2843b4d711fdc468c4e9bc..87cb646178974111ec4fecb845d1189e56819185 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 6eb562667b50c5df05d1d4a05ef5337f4b29387c..24a13280ae0d4884462bf4b36817b4e0948b4f46 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 0841e9095170ae18c60fd4f95c563b39235f077a..0cf80ca2a58009b4055a599fac3a7d4d45a2e799 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 } }; }