Skip to content
Snippets Groups Projects
Commit 90de7839 authored by Gundo Sifhufhi's avatar Gundo Sifhufhi
Browse files

Add Extensions Attribute `Suburb`

Layout Processor For Handling Field Display.
Dynamically add columns to Magento Core DB.
Save Suburb to Quote Table.
parent 512162cc
No related branches found
No related tags found
No related merge requests found
Showing with 217 additions and 27 deletions
......@@ -5,6 +5,7 @@ namespace uafrica\Customshipping\Model\Carrier;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Directory\Helper\Data;
use Magento\Directory\Model\CountryFactory;
use Magento\Directory\Model\CurrencyFactory;
......@@ -31,7 +32,7 @@ use Psr\Log\LoggerInterface;
/**
* uAfrica shipping implementation
* @category uafrica
* @category bob
* @package uafrica_Customshipping
* @author info@bob.co.za
* @website https://www.bob.co.za
......@@ -48,10 +49,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
*/
public const CODE = 'uafrica';
/** Tracking Endpoint */
const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=';
/*** RATES API Endpoint*/
const RATES_ENDPOINT = 'https://api.dev.ship.uafrica.com/rates-at-checkout/woocommerce';
const UNITS = 100;
/**
* Code of the carrier
......@@ -108,6 +106,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
*/
protected JsonFactory $jsonFactory;
private $cartRepository;
/**
......@@ -212,6 +211,15 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
}
public function beforeSaveAddressInformation($subject, $cartId, ShippingInformationInterface $addressInformation)
{
$quote = $this->cartRepository->getActive($cartId);
$deliveryNote = $addressInformation->getShippingAddress()->getExtensionAttributes()->getSuburb();
$quote->setSuburb($deliveryNote);
//$this->cartRepository->save($quote);
return $addressInformation;
}
/**
* Collect and get rates
*
......@@ -225,6 +233,9 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateFactory->create();
......@@ -235,9 +246,9 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
$destCity = $request->getDestCity();
$destStreet = $request->getDestStreet() !== null ? str_replace("\n", ' ', $request->getDestStreet()) : '';
$destStreet1 = $destStreet;
// $destStreet2 = $destStreet;
//Get all the origin data from the request
/** Origin Information */
list($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb) = $this->storeInformation();
......@@ -271,7 +282,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
'destination' => [
'company' => '', // TODO :: Add this if available
'address1' => $destStreet1,
'address2' => '',
'suburb' => '',
'city' => $destCity,
'province' => $destRegion,
'country_code' => $destCountry,
......@@ -360,7 +371,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
protected function _getPerpackagePrice($cost, $handlingType, $handlingFee)
{
if ($handlingType == AbstractCarrier::HANDLING_TYPE_PERCENT) {
return $cost + $cost * $this->_numBoxes * $handlingFee / 100;
return $cost + $cost * $this->_numBoxes * $handlingFee / self::UNITS;
}
return $cost + $this->_numBoxes * $handlingFee;
......@@ -377,7 +388,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
protected function _getPerorderPrice($cost, $handlingType, $handlingFee)
{
if ($handlingType == self::HANDLING_TYPE_PERCENT) {
return $cost + $cost * $handlingFee / 100;
return $cost + $cost * $handlingFee / self::UNITS;
}
return $cost + $handlingFee;
......@@ -489,7 +500,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
$tracking->setCarrier(self::CODE);
$tracking->setCarrierTitle($carrierTitle);
$tracking->setUrl(self::TRACKING.$item);
$tracking->setUrl(uData::TRACKING .$item);
$tracking->setTracking($item);
$tracking->addData($this->processTrackingDetails($item));
$result->append($tracking);
......@@ -693,7 +704,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
*/
private function getApiUrl(): string
{
return self::RATES_ENDPOINT;
return uData::RATES_ENDPOINT;
}
/**
......@@ -741,23 +752,26 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
$result->append($error);
} else {
foreach ($rates['rates'] as $code => $title) {
foreach ($rates['rates'] as $title) {
$method = $this->_rateMethodFactory->create();
if (isset($title)){
$method->setCarrier(self::CODE);
if ($this->getConfigData('additional_info') == 1) {
$min_delivery_date = $this->getWorkingDays(date('Y-m-d'), $title['min_delivery_date']);
$max_delivery_date = $this->getWorkingDays(date('Y-m-d'), $title['max_delivery_date']);
$method->setCarrierTitle('Delivery in ' . $min_delivery_date .' - ' . $max_delivery_date .' Business Days');
} else {
$method->setCarrierTitle($this->getConfigData('title'));
}
}
$method->setMethod($title['service_code']);
$method->setMethodTitle($title['service_name']);
$method->setPrice($title['total_price'] / 100);
$method->setCost($title['total_price'] / 100);
$method->setPrice($title['total_price'] / self::UNITS);
$method->setCost($title['total_price'] / self::UNITS);
$result->append($method);
}
......@@ -821,7 +835,7 @@ class Customshipping extends AbstractCarrierOnline implements \Magento\Shipping\
*/
private function trackUafricaShipment($trackInfo): mixed
{
$this->curl->get(self::TRACKING . $trackInfo);
$this->curl->get(uData::TRACKING . $trackInfo);
$response = $this->curl->getBody();
......
<?php
namespace uafrica\Customshipping\Model\Carrier;
/**
* Class uData
* @package uafrica\Customshipping\Model\Carrier
*/
class uData
{
/** Tracking Endpoint */
public const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=';
/*** RATES API Endpoint*/
public const RATES_ENDPOINT = 'https://api.dev.ship.uafrica.com/rates-at-checkout/woocommerce';
}
<?php
namespace uafrica\Customshipping\Observer;
use Magento\Framework\DataObject\Copy;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote;
use Magento\Sales\Model\Order;
class SaveOrderBeforeSalesModelQuote implements ObserverInterface
{
public Copy $objectCopyService;
public function __construct(
Copy $objectCopyService
) {
$this->objectCopyService = $objectCopyService;
}
public function execute(Observer $observer)
{
$this->objectCopyService->copyFieldsetToTarget(
'sales_convert_quote',
'to_order',
$observer->getEvent()->getQuote(),
$observer->getEvent()->getOrder()
);
return $this;
}
}
<?php
namespace uafrica\Customshipping\Plugin\Checkout\Block;
use Magento\Checkout\Block\Checkout\LayoutProcessor;
class LayoutProcessorPlugin
{
/**
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* This is the Class That Allows The Field To Appear As Required On Checkout,
* But It Does Not Save The Data To The Quote Table In The Database For The Order
* To Be Processed Correctly In The Backend And Frontend It Works With Shipping
* Information Management Class
* @param LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
LayoutProcessor $subject,
array $jsLayout
) {
): array
{
$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',
['shippingAddress']['children']['shipping-address-fieldset']['children']['suburb'] = [
'component' => 'Magento_Ui/js/form/element/textarea',
'config' => [
'customScope' => 'shippingAddress.custom_attributes',
'customScope' => 'shippingAddress.extension_attributes',
'customEntry' => null,
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input',
'options' => [],
'tooltip' => [
'description' => __('Necessary for shipping.')
],
'id' => 'suburb'
],
'dataScope' => 'shippingAddress.custom_attributes.custom_field_text',
'dataScope' => 'shippingAddress.extension_attributes.suburb',
'label' => 'Suburb',
'provider' => 'checkoutProvider',
'visible' => true,
......
<?php
namespace uafrica\Customshipping\Plugin;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Checkout\Api\Data\ShippingInformationInterface;
class ShippingInformationManagement
{
public CartRepositoryInterface $cartRepository;
public function __construct(
CartRepositoryInterface $cartRepository,
)
{
$this->cartRepository = $cartRepository;
}
public function beforeSaveAddressInformation($subject, $cartId, ShippingInformationInterface $addressInformation)
{
$quote = $this->cartRepository->getActive($cartId);
$deliveryNote = $addressInformation->getShippingAddress()->getExtensionAttributes()->getSuburb();
$quote->setSuburb($deliveryNote);
$this->cartRepository->save($quote);
return [$cartId, $addressInformation];
}
}
......@@ -2,7 +2,7 @@
"name": "uafrica/bobgo",
"description": "Streamline your shipments fulfillments with bobgo",
"type": "magento2-module",
"version": "1.0.1",
"version": "1.0.0",
"authors": [
{
"name": "Gundo",
......
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="quote" resource="default" engine="innodb">
<column xsi:type="text" name="suburb" nullable="true" comment="Delivery suburb"/>
<column xsi:type="text" name="service_code" nullable="true" comment="Shipping Service Code"/>
<column xsi:type="text" name="delivery_note" nullable="true" comment="Delivery Note"/>
</table>
<table name="sales_order" resource="default" engine="innodb">
<column xsi:type="text" name="suburb" nullable="true" comment="Delivery Suburb"/>
<column xsi:type="text" name="service_code" nullable="true" comment="Shipping Service Code"/>
<column xsi:type="text" name="delivery_note" nullable="true" comment="Delivery Note"/>
</table>
<table name="customer_address_entity" resource="default" engine="innodb">
<column xsi:type="text" name="suburb" nullable="true" comment="Customer Suburb"/>
<column xsi:type="text" name="service_code" nullable="true" comment="Shipping Service Code"/>
<column xsi:type="text" name="delivery_note" nullable="true" comment="Delivery Note"/>
</table>
</schema>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\ShippingInformationManagement">
<plugin name="get_suburb"
type="uafrica\Customshipping\Plugin\ShippingInformationManagement" sortOrder="1"/>
</type>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="sales_model_service_quote_submit_before">
<observer name="uafrica_customshipping_sales_model_service_quote_submit_before"
instance="uafrica\Customshipping\Observer\SaveOrderBeforeSalesModelQuote" />
</event>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
<attribute code="suburb" type="string"/>
</extension_attributes>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
<fieldset id="sales_convert_quote">
<field name="suburb">
<aspect name="to_order" />
</field>
</fieldset>
</scope>
</config>
<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"/>
<arguments>
<argument name="layoutProcessors" xsi:type="array">
<item name="orderattribute" xsi:type="object">uafrica\Customshipping\Plugin\Checkout\Block\LayoutProcessorPlugin</item>
</argument>
</arguments>
</type>
</config>
/*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;
// pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
return originalAction();
});
};
});
......@@ -18,6 +18,9 @@ define(
},
'city': {
'required': true
},
'suburb': {
'required': true
}
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment