From 75bed96ee7eb4b672b9799e11edd87298dfc308d Mon Sep 17 00:00:00 2001 From: Gundo Sifhufhi <sifhufhisg@gmail.com> Date: Tue, 14 Feb 2023 12:24:32 +0200 Subject: [PATCH] Added functionality to default the extension to ZA. Removed system.xml fields. --- Block/System/Config/Form/Field/Version.php | 10 +-- Model/Carrier/CustomShipping.php | 79 +++++++++++++--------- Model/Carrier/uData.php | 8 ++- Model/Source/Allspecificcountries.php | 23 +++++++ Model/Source/Country.php | 13 ++++ Model/Source/Generic.php | 2 +- Plugin/ShippingInformationManagement.php | 2 +- etc/adminhtml/system.xml | 48 +++++-------- etc/config.xml | 4 +- 9 files changed, 115 insertions(+), 74 deletions(-) create mode 100644 Model/Source/Allspecificcountries.php create mode 100644 Model/Source/Country.php diff --git a/Block/System/Config/Form/Field/Version.php b/Block/System/Config/Form/Field/Version.php index 262bf7d..964f53c 100644 --- a/Block/System/Config/Form/Field/Version.php +++ b/Block/System/Config/Form/Field/Version.php @@ -5,14 +5,14 @@ use Magento\Framework\Data\Form\Element\AbstractElement; /** * Displays Version number in System Configuration - * @category Bob Go + * @category BobGroup * @package bobgo_CustomShipping - * @author Bob Go - * @website https://www.bobgo.co.za + * @author info@bob.co.za + * @website https://www.bob.co.za */ class Version extends \Magento\Config\Block\System\Config\Form\Field { - const EXTENSION_URL = 'https://www.bobgo.co.za'; + const EXTENSION_URL = 'https://www.bob.co.za'; /** * @var \bobgo\CustomShipping\Helper\Data $helper @@ -39,7 +39,7 @@ class Version extends \Magento\Config\Block\System\Config\Form\Field protected function _getElementHtml(AbstractElement $element) { $extensionVersion = $this->_helper->getExtensionVersion(); - $extensionTitle = 'Bob Go'; + $extensionTitle = 'BobGo'; $versionLabel = sprintf( '<a href="%s" title="%s" target="_blank">%s</a>', self::EXTENSION_URL, diff --git a/Model/Carrier/CustomShipping.php b/Model/Carrier/CustomShipping.php index af748b6..1cf940d 100644 --- a/Model/Carrier/CustomShipping.php +++ b/Model/Carrier/CustomShipping.php @@ -22,7 +22,6 @@ use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory; use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Magento\Shipping\Model\Carrier\AbstractCarrier; use Magento\Shipping\Model\Carrier\AbstractCarrierOnline; -use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\Result; use Magento\Shipping\Model\Rate\ResultFactory; use Magento\Shipping\Model\Simplexml\ElementFactory; @@ -41,7 +40,7 @@ use Psr\Log\LoggerInterface; * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) */ -class CustomShipping extends AbstractCarrierOnline implements CarrierInterface +class CustomShipping extends AbstractCarrierOnline implements \Magento\Shipping\Model\Carrier\CarrierInterface { /** * Code of the carrier @@ -192,6 +191,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface return parse_url($storeBaseUrl)["host"]; } + /** * Make request to Bob Go API to get shipping rates for the cart * After all the required data is collected, it makes a request to the Bob Go API to get the shipping rates @@ -209,9 +209,9 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @param RateRequest $request * @return Result|bool|null */ - public function collectRates(RateRequest $request): Result|bool|null + public function collectRates(RateRequest $request) { - /*** Make sure that Shipping method is enabled*/ + /*** Make sure that Shipping method is enabled*/ if (!$this->isActive()) { return false; } @@ -221,7 +221,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface */ $destComp = $this->getDestComp(); - /** @var Result $result */ + /** @var \Magento\Shipping\Model\Rate\Result $result */ $result = $this->_rateFactory->create(); @@ -332,16 +332,16 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface ); $baseIdentifier = $this->getBaseUrl(); - return array($originStreet, $originRegion, $originCountry, $originCity, $originStreet1, $originStreet2, $storeName, $baseIdentifier, $originSuburb); } + /** * Get result of request * * @return Result|null */ - public function getResult(): ?Result + public function getResult() { if (!$this->_result) { $this->_result = $this->_trackFactory->create(); @@ -349,6 +349,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface return $this->_result; } + /** * Get final price for shipping method with handling fee per package * @@ -357,7 +358,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @param float $handlingFee * @return float */ - protected function _getPerpackagePrice($cost, $handlingType, $handlingFee): float + protected function _getPerpackagePrice($cost, $handlingType, $handlingFee) { if ($handlingType == AbstractCarrier::HANDLING_TYPE_PERCENT) { return $cost + $cost * $this->_numBoxes * $handlingFee / self::UNITS; @@ -374,7 +375,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @param float $handlingFee * @return float */ - protected function _getPerorderPrice($cost, $handlingType, $handlingFee): float + protected function _getPerorderPrice($cost, $handlingType, $handlingFee) { if ($handlingType == self::HANDLING_TYPE_PERCENT) { return $cost + $cost * $handlingFee / self::UNITS; @@ -383,29 +384,30 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface return $cost + $handlingFee; } + /** * Get configuration data of carrier - * + * Interact with the * @param string $type * @param string $code * @return array|false * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function getCode($type, $code = ''): bool|array + public function getCode($type, $code = '') { $codes = [ 'method' => [ - 'bobgo' => __('Bob Go'), - ], - 'delivery_confirmation_types' => [ - 'NO_SIGNATURE_REQUIRED' => __('Not Required'), - 'ADULT' => __('Adult'), - 'DIRECT' => __('Direct'), - 'INDIRECT' => __('Indirect'), + 'bobgo_SHIPPING' => __('bobgo Shipping'), ], 'unit_of_measure' => [ 'KG' => __('Kilograms'), ], + 'specificcountry' => [ + 'ZA' => __('South Africa'), + ], + 'allspecificcountries' => [ + 'ZA' => __('South Africa'), + ], ]; if (!isset($codes[$type])) { @@ -421,6 +423,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface } } + /** * Get tracking info by tracking number or tracking request * Without getTrackingInfo() method, Magento will not show tracking info on frontend @@ -428,7 +431,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @param string|string[] $trackings * @return Result|null */ - public function getTracking($trackings): ?Result + public function getTracking($trackings) { $this->setTrackingReqeust(); @@ -448,7 +451,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * * @return void */ - protected function setTrackingReqeust(): void + protected function setTrackingReqeust() { $r = new \Magento\Framework\DataObject(); @@ -464,18 +467,18 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @param string[] $tracking * @return void */ - protected function _getXMLTracking($tracking): void + protected function _getXMLTracking($tracking) { $this->_parseTrackingResponse($tracking); } /** - * Parse tracking response and set tracking info + * Parse tracking response * * @param string $trackingValue * @return void */ - protected function _parseTrackingResponse($trackingValue): void + protected function _parseTrackingResponse($trackingValue) { $result = $this->getResult(); $carrierTitle = $this->getConfigData('title'); @@ -487,12 +490,20 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface $tracking = $this->_trackStatusFactory->create(); $tracking->setCarrier(self::CODE); $tracking->setCarrierTitle($carrierTitle); - $tracking->setUrl(sprintf(uData::TRACKING, $this->getBaseUrl(), $trackingReference)); + + //Production + /* $tracking->setUrl(sprintf(uData::TRACKING, $this->getBaseUrl(), $trackingReference)); $tracking->setTracking($trackingReference); $tracking->addData($this->processTrackingDetails($trackingReference)); + */ + + //Dev + $tracking->setUrl(uData::TRACKING .$trackingReference); + $tracking->setTracking($trackingReference); + $tracking->addData($this->processTrackingDetails($trackingReference)); + $result->append($tracking); $counter ++; - echo (sprintf(uData::TRACKING, $this->getBaseUrl(), $trackingReference)); } //Tracking Details Not Available @@ -509,7 +520,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * * @return string */ - public function getResponse(): string + public function getResponse() { $statuses = ''; if ($this->_result instanceof \Magento\Shipping\Model\Tracking\Result) { @@ -538,7 +549,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * * @return array */ - public function getAllowedMethods(): array + public function getAllowedMethods() { $allowed = explode(',', $this->getConfigData('allowed_methods')); $arr = []; @@ -549,13 +560,14 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface return $arr; } + /** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response * Also another magic function that is required to be implemented by carrier model * @param \Magento\Framework\DataObject $request * @return \Magento\Framework\DataObject */ - protected function _doShipmentRequest(\Magento\Framework\DataObject $request): ?DataObject + protected function _doShipmentRequest(\Magento\Framework\DataObject $request) { return null; @@ -568,7 +580,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * * @return bool */ - public function rollBack($data): bool + public function rollBack($data) { return true; } @@ -581,7 +593,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface * @return array|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function getContainerTypes(\Magento\Framework\DataObject $params = null): bool|array + public function getContainerTypes(\Magento\Framework\DataObject $params = null) { $result = []; $allowedContainers = $this->getConfigData('containers'); @@ -717,7 +729,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface */ private function _requestTracking($trackInfo, array $result): array { - $response = $this->trackBobGoShipment($trackInfo); + $response = $this->trackbobgoShipment($trackInfo); $result = $this->prepareActivity($response[0], $result); @@ -768,6 +780,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface } } + /** * Prepare received checkpoints and activity from Bob Go Shipment Tracking API * @param $response @@ -816,12 +829,13 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface } } + /** * Curl request to Bob Go Shipment Tracking API * @param $trackInfo * @return mixed */ - private function trackBobGoShipment($trackInfo): mixed + private function trackbobgoShipment($trackInfo): mixed { $this->curl->get(uData::TRACKING . $trackInfo); @@ -846,6 +860,7 @@ class CustomShipping extends AbstractCarrierOnline implements CarrierInterface return $rates; } + /** * @param string $destStreet * @return string[] diff --git a/Model/Carrier/uData.php b/Model/Carrier/uData.php index 8ea4070..514572e 100644 --- a/Model/Carrier/uData.php +++ b/Model/Carrier/uData.php @@ -4,13 +4,17 @@ namespace bobgo\CustomShipping\Model\Carrier; /** * Class uData - * Bob Go API Resources for Custom Shipping + * bobGo API Resources for Custom Shipping * @package bobgo\CustomShipping\Model\Carrier */ class uData { /** Tracking Endpoint */ - public const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=%s&tracking_reference=%s'; + //dev + public const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference='; + //production + // public const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=%s&tracking_reference=%s'; + /*** RATES API Endpoint*/ public const RATES_ENDPOINT = 'https://api.dev.ship.uafrica.com/rates-at-checkout/magento'; } diff --git a/Model/Source/Allspecificcountries.php b/Model/Source/Allspecificcountries.php new file mode 100644 index 0000000..eecbda8 --- /dev/null +++ b/Model/Source/Allspecificcountries.php @@ -0,0 +1,23 @@ +<?php + +namespace bobgo\CustomShipping\Model\Source; + +class Allspecificcountries extends Generic +{ + /** + * Carrier code + * @var string + */ + protected string $_code = 'allspecificcountries'; + + /** + * @return array + */ + public function toOptionArray(): array + { + return [ + ['value' => 1, 'label' => __('Specific Countries')] + ]; + } + +} diff --git a/Model/Source/Country.php b/Model/Source/Country.php new file mode 100644 index 0000000..017d735 --- /dev/null +++ b/Model/Source/Country.php @@ -0,0 +1,13 @@ +<?php + +namespace bobgo\CustomShipping\Model\Source; + +class Country extends Generic +{ + /** + * Carrier code + * @var string + */ + protected string $_code = 'specificcountry'; + +} diff --git a/Model/Source/Generic.php b/Model/Source/Generic.php index 8bebd17..4751ab0 100644 --- a/Model/Source/Generic.php +++ b/Model/Source/Generic.php @@ -20,7 +20,7 @@ class Generic implements OptionSourceInterface * Carrier code * @var string */ - protected string $_code = ''; + protected string $_code = 'bobgo'; /** * @param CustomShipping $shippingCustomShipping diff --git a/Plugin/ShippingInformationManagement.php b/Plugin/ShippingInformationManagement.php index 233ed2c..107f90b 100644 --- a/Plugin/ShippingInformationManagement.php +++ b/Plugin/ShippingInformationManagement.php @@ -16,7 +16,7 @@ class ShippingInformationManagement public CartRepositoryInterface $cartRepository; public function __construct( - CartRepositoryInterface $cartRepository + CartRepositoryInterface $cartRepository, ) { $this->cartRepository = $cartRepository; diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 358c894..2174008 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -8,16 +8,16 @@ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> - <tab id="bobgo" translate="label" sortOrder="200"> + + <tab id="BobGo" translate="label" sortOrder="20"> <label>Bob Go</label> </tab> - <section id="bobgo_CustomShipping" translate="label" type="text" sortOrder="520" showInDefault="1" - showInWebsite="1" showInStore="0"> - <class>separator-top</class> - <tab>bobgo</tab> + + <section id="bobgo_CustomShipping" showInDefault="1"> + <tab>BobGo</tab> <label>Settings</label> <resource>Magento_Config::config</resource> - <group id="general" showInDefault="1"> + <group id="general" showInDefault="1"> <label>Settings</label> <field id="version" translate="label" type="label" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Version</label> @@ -27,12 +27,10 @@ <label>Extension enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> - <!--field id="debug" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> - <label>Debug Mode</label> - <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> - </field--> </group> </section> + +<!-- General Section(Store Information) --> <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"> @@ -41,35 +39,31 @@ </field> </group> </section> + <!--- End General Section --> + +<!--- Delivery Methods Section --> <section id="carriers" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <group id="bobgo" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Bob Go</label> + <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Enabled for Checkout</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> - </field> - - <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1"> - <label>Title</label> - </field> - - <field id="residence_delivery" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1"> - <label>Residential Delivery</label> - <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> + <comment>Enable this shipping method for your customers</comment> </field> <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Displayed Error Message</label> + <comment>Displayed if the extension fails to calculate the shipping cost</comment> </field> <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Ship to Applicable Countries</label> <frontend_class>shipping-applicable-country</frontend_class> - <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model> + <source_model>bobgo\CustomShipping\Model\Source\Allspecificcountries</source_model> </field> - - <field id="specificcountry" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0"> + <field id="specificcountry" translate="label" type="select" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Ship to Specific Countries</label> - <source_model>Magento\Directory\Model\Config\Source\Country</source_model> + <source_model>bobgo\CustomShipping\Model\Source\Country</source_model> <can_be_empty>1</can_be_empty> </field> @@ -87,14 +81,6 @@ <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> <comment>Display additional information on the checkout page</comment> </field> - <field id="debug" translate="label" type="select" sortOrder="1950" showInDefault="1" showInWebsite="1"> - <label>Debug</label> - <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> - </field> - <field id="sandbox_mode" translate="label" type="select" sortOrder="1960" showInDefault="1" showInWebsite="1"> - <label>Sandbox Mode</label> - <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> - </field> </group> </section> diff --git a/etc/config.xml b/etc/config.xml index d31cdc5..aa8f79d 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -1,9 +1,9 @@ <?xml version="1.0"?> <!-- /** - * @category Bob Go + * @category bobgo * @package bobgo_CustomShipping - * @author Bob Go + * @author bobgo */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> -- GitLab