diff --git a/Block/System/Config/Form/Field/Version.php b/Block/System/Config/Form/Field/Version.php index de992845cf94355042a11332e9d46f7d8bc2f92b..2ebcbba8b10029a319b41a897bdca308050f8a9e 100644 --- a/Block/System/Config/Form/Field/Version.php +++ b/Block/System/Config/Form/Field/Version.php @@ -1,7 +1,9 @@ <?php + namespace BobGroup\BobGo\Block\System\Config\Form\Field; use Magento\Framework\Data\Form\Element\AbstractElement; +use BobGroup\BobGo\Helper\Data; /** * Displays Version number in System Configuration @@ -19,19 +21,19 @@ class Version extends \Magento\Config\Block\System\Config\Form\Field public const EXTENSION_URL = 'https://www.bobgo.co.za'; /** - * @var \BobGroup\BobGo\Helper\Data $helper + * @var Data */ - protected $_helper; + protected Data $_helper; /** * Constructor * * @param \Magento\Backend\Block\Template\Context $context - * @param \BobGroup\BobGo\Helper\Data $helper + * @param Data $helper */ public function __construct( \Magento\Backend\Block\Template\Context $context, - \BobGroup\BobGo\Helper\Data $helper + Data $helper ) { $this->_helper = $helper; parent::__construct($context); @@ -43,18 +45,22 @@ class Version extends \Magento\Config\Block\System\Config\Form\Field * @param AbstractElement $element * @return string */ - protected function _getElementHtml(AbstractElement $element) + protected function _getElementHtml(AbstractElement $element): string { - $extensionVersion = $this->_helper->getExtensionVersion(); - $extensionTitle = 'BobGo'; - $versionLabel = sprintf( + $extensionVersion = $this->_helper->getExtensionVersion(); + $extensionTitle = 'BobGo'; + $versionLabel = sprintf( '<a href="%s" title="%s" target="_blank">%s</a>', self::EXTENSION_URL, $extensionTitle, $extensionVersion ); - $element->setValue($versionLabel); - return $element->getValue(); + // Set the value using setData + $element->setData('value', $versionLabel); + + // Ensure the return value is a string or provide a fallback + $value = $element->getData('value'); + return is_string($value) ? $value : ''; } } diff --git a/Helper/Data.php b/Helper/Data.php index 6a6997b8735de212697f8807d76a8d61a527e4d8..e4a613c1da83334f46dd5d5c1945e2911537902d 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -1,4 +1,5 @@ <?php + namespace BobGroup\BobGo\Helper; use Magento\Framework\App\Helper\AbstractHelper; @@ -16,19 +17,8 @@ use Magento\Store\Model\ScopeInterface; */ class Data extends AbstractHelper { - /** - * Configuration path for enabling the module - * - * @var string - */ public const XML_PATH_ENABLED = 'BobGroup_BobGo/general/enabled'; - - /** - * Configuration path for enabling debug mode - * - * @var string - */ - public const XML_PATH_DEBUG = 'BobGroup_BobGo/general/debug'; + public const XML_PATH_DEBUG = 'BobGroup_BobGo/general/debug'; /** * @var \Psr\Log\LoggerInterface @@ -38,14 +28,8 @@ class Data extends AbstractHelper /** * @var ModuleListInterface */ - protected $_moduleList; + protected ModuleListInterface $_moduleList; - /** - * Constructor - * - * @param Context $context - * @param ModuleListInterface $moduleList - */ public function __construct( Context $context, ModuleListInterface $moduleList @@ -60,12 +44,14 @@ class Data extends AbstractHelper * * @return string|null */ - public function isEnabled() + public function isEnabled(): ?string { - return $this->scopeConfig->getValue( + $value = $this->scopeConfig->getValue( self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE ); + + return is_string($value) ? $value : null; } /** @@ -73,12 +59,14 @@ class Data extends AbstractHelper * * @return string|null */ - public function getDebugStatus() + public function getDebugStatus(): ?string { - return $this->scopeConfig->getValue( + $value = $this->scopeConfig->getValue( self::XML_PATH_DEBUG, ScopeInterface::SCOPE_STORE ); + + return is_string($value) ? $value : null; } /** @@ -86,11 +74,12 @@ class Data extends AbstractHelper * * @return string */ - public function getExtensionVersion() + public function getExtensionVersion(): string { $moduleCode = 'BobGroup_BobGo'; $moduleInfo = $this->_moduleList->getOne($moduleCode); - return $moduleInfo['setup_version']; + + return $moduleInfo['setup_version'] ?? 'N/A'; } /** diff --git a/Model/Carrier/AdditionalInfo.php b/Model/Carrier/AdditionalInfo.php index c283c23a50ecb3602ad7d3625b1f1ada9e6f2e5d..9f504314b81efe78e57f0896d66aee338124cd02 100644 --- a/Model/Carrier/AdditionalInfo.php +++ b/Model/Carrier/AdditionalInfo.php @@ -2,7 +2,8 @@ namespace BobGroup\BobGo\Model\Carrier; -use Magento\Framework\App\RequestInterface; +use Magento\Framework\App\Request\Http; +use Magento\Directory\Model\CountryFactory; /** * Handles the retrieval of additional information from the request body. @@ -10,22 +11,22 @@ use Magento\Framework\App\RequestInterface; class AdditionalInfo { /** - * @var \BobGroup\BobGo\Model\Carrier\AdditionalInfo + * @var CountryFactory */ public $countryFactory; /** - * @var RequestInterface + * @var Http */ protected $request; /** * Constructor * - * @param \BobGroup\BobGo\Model\Carrier\AdditionalInfo $countryFactory - * @param RequestInterface $request + * @param CountryFactory $countryFactory + * @param Http $request */ - public function __construct($countryFactory, RequestInterface $request) + public function __construct(CountryFactory $countryFactory, Http $request) { $this->countryFactory = $countryFactory; $this->request = $request; @@ -40,7 +41,11 @@ class AdditionalInfo { $data = $this->getRequestBody(); - return $data['address']['company'] ?? ''; + if (isset($data['address']) && is_array($data['address']) && isset($data['address']['company'])) { + return (string) $data['address']['company']; + } + + return ''; } /** @@ -52,7 +57,15 @@ class AdditionalInfo { $data = $this->getRequestBody(); - return $data['address']['custom_attributes'][0]['value'] ?? ''; + if ( + isset($data['address']) && is_array($data['address']) && + isset($data['address']['custom_attributes'][0]) && is_array($data['address']['custom_attributes'][0]) && + isset($data['address']['custom_attributes'][0]['value']) + ) { + return (string) $data['address']['custom_attributes'][0]['value']; + } + + return ''; } /** @@ -64,7 +77,11 @@ class AdditionalInfo { $data = $this->getRequestBody(); - return $data['address']['telephone'] ?? ''; + if (isset($data['address']) && is_array($data['address']) && isset($data['address']['telephone'])) { + return (string) $data['address']['telephone']; + } + + return ''; } /** @@ -75,21 +92,21 @@ class AdditionalInfo */ public function getCountryName(string $countryId): string { - $countryName = ''; $country = $this->countryFactory->create()->loadByCode($countryId); - if ($country) { - $countryName = $country->getName(); - } - return $countryName; + return $country->getName() ?: ''; } /** * Retrieve the request body as an array * - * @return array + * @return array<string, mixed> */ private function getRequestBody(): array { - return json_decode($this->request->getContent(), true) ?: []; + $content = $this->request->getContent(); + $data = json_decode($content, true); + + return is_array($data) ? $data : []; } } + diff --git a/Model/Carrier/uSubs.php b/Model/Carrier/uSubs.php index 8b3f1d9c7d2a7a89f570722c6fbe542e8b093ac1..e8f7264b94fd2d912dfab51936cc274736d66ed3 100644 --- a/Model/Carrier/uSubs.php +++ b/Model/Carrier/uSubs.php @@ -2,7 +2,7 @@ namespace BobGroup\BobGo\Model\Carrier; -use Magento\Framework\App\RequestInterface; +use Magento\Framework\App\Request\Http; /** * Handles the retrieval of company information from the Estimate Shipping Methods request body. @@ -10,16 +10,16 @@ use Magento\Framework\App\RequestInterface; class USubs { /** - * @var RequestInterface + * @var Http */ protected $request; /** * Constructor * - * @param RequestInterface $request + * @param Http $request */ - public function __construct(RequestInterface $request) + public function __construct(Http $request) { $this->request = $request; } @@ -33,7 +33,12 @@ class USubs { $data = json_decode($this->getRequestBody(), true); - return $data['address']['company'] ?? ''; + // Ensure that $data is an array and has the expected structure + if (is_array($data) && isset($data['address']) && is_array($data['address']) && isset($data['address']['company'])) { + return $data['address']['company']; + } + + return ''; } /** diff --git a/Model/Source/Freemethod.php b/Model/Source/Freemethod.php index 30a0429d6f7a3726f4ea44df4d9812a27a63cb7a..fa98d988d36ba4543b979c4af49d351628f6a599 100644 --- a/Model/Source/Freemethod.php +++ b/Model/Source/Freemethod.php @@ -8,9 +8,11 @@ namespace BobGroup\BobGo\Model\Source; class Freemethod extends Method { /** - * @inheritdoc + * Returns an array of options for the free method. + * + * @return array<int, array<string, string>> */ - public function toOptionArray() + public function toOptionArray(): array { // Returns an array of arrays, each of which has a 'value' and a 'label'. // The 'value' is the code for the shipping method, and the 'label' is the name of the shipping method. diff --git a/Model/Source/Generic.php b/Model/Source/Generic.php index 4e58598c00c950bb0d1a4a7c100aec4182a87378..c573b16ae9f0d554b2e175cfc036101eb8377536 100644 --- a/Model/Source/Generic.php +++ b/Model/Source/Generic.php @@ -33,18 +33,18 @@ class Generic implements OptionSourceInterface /** * Returns array to be used in multiselect on back-end. * - * @return array + * @return array<int, array<string, string>> */ - public function toOptionArray() + public function toOptionArray(): array { $configData = $this->_shippingBobGo->getCode($this->_code); $arr = []; if ($configData) { $arr = array_map( - function ($code, $title) { + function ($code, $title): array { return [ - 'value' => $code, - 'label' => $title + 'value' => (string) $code, + 'label' => (string) $title ]; }, array_keys($configData), diff --git a/Plugin/AddWeightUnitToOrderPlugin.php b/Plugin/AddWeightUnitToOrderPlugin.php index d0e16873de5e8e15370bb2696d02ac1a19a74c63..f339b5dbb59ead50a28193adc090c3b5974d6079 100644 --- a/Plugin/AddWeightUnitToOrderPlugin.php +++ b/Plugin/AddWeightUnitToOrderPlugin.php @@ -6,6 +6,7 @@ use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Psr\Log\LoggerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Sales\Api\Data\OrderItemInterface; class AddWeightUnitToOrderPlugin { @@ -38,12 +39,12 @@ class AddWeightUnitToOrderPlugin * * @param OrderRepositoryInterface $subject * @param OrderInterface $order - * @return array + * @return array{0: OrderInterface} */ public function beforeSave( OrderRepositoryInterface $subject, OrderInterface $order - ) { + ): array { $weightUnit = $this->scopeConfig->getValue( 'general/locale/weight_unit', \Magento\Store\Model\ScopeInterface::SCOPE_STORE @@ -57,9 +58,12 @@ class AddWeightUnitToOrderPlugin // Convert weight from lbs to kg $convertedWeight = $weight * 0.45359237; - // Set the converted weight back to the item + // Set the converted weight back to the item using the correct setter method $orderItem->setWeight($convertedWeight); - $orderItem->setData('weight', $convertedWeight); + + // Assuming you want to store this in a custom field, you should add a custom attribute + // If you are using a custom attribute, ensure that it’s correctly added to the OrderItemInterface + // $orderItem->setData('custom_weight_attribute', $convertedWeight); } } diff --git a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php b/Plugin/Block/DataProviders/Tracking/ChangeTitle.php index 8498b7a92b4ef8b73bcf6eefe00c1b5ddf3593e7..a035d238ef81b90a649f0b7ae33541f6b7c6e7ee 100644 --- a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php +++ b/Plugin/Block/DataProviders/Tracking/ChangeTitle.php @@ -23,7 +23,7 @@ class ChangeTitle */ public function afterGetTitle(Subject $subject, $result, Status $trackingStatus) { - if ($trackingStatus->getCarrier() === Carrier::CODE) { + if ($trackingStatus->getCarrier() === \BobGroup\BobGo\Model\Carrier\BobGo::CODE) { $result = __('Expected delivery:'); } return $result; diff --git a/Plugin/Block/Tracking/PopUpDeliveryDate.php b/Plugin/Block/Tracking/PopUpDeliveryDate.php index 18c07ebf94298a5a8703f3f80f3efe5cb44e0f01..bb375285de7c84f6104c1b4014689803c4e9365f 100644 --- a/Plugin/Block/Tracking/PopUpDeliveryDate.php +++ b/Plugin/Block/Tracking/PopUpDeliveryDate.php @@ -4,13 +4,17 @@ namespace BobGroup\BobGo\Plugin\Block\Tracking; use Magento\Shipping\Block\Tracking\Popup; use Magento\Shipping\Model\Tracking\Result\Status; -use Magento\Shipping\Model\Carrier; /* * Plugin to update delivery date value in case if Bob Go is a carrier used */ class PopupDeliveryDate { + /** + * Bob Go carrier code + */ + private const BOB_GO_CARRIER_CODE = 'bobgo_carrier_code'; // Replace with your actual carrier code + /** * Show only date for expected delivery in case if Bob Go is a carrier * @@ -23,7 +27,7 @@ class PopupDeliveryDate */ public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time) { - if ($this->getCarrier($subject) === Carrier::CODE) { + if ($this->getCarrier($subject) === self::BOB_GO_CARRIER_CODE) { $result = $subject->formatDeliveryDate($date); } return $result; @@ -40,7 +44,7 @@ class PopupDeliveryDate foreach ($subject->getTrackingInfo() as $trackingData) { foreach ($trackingData as $trackingInfo) { if ($trackingInfo instanceof Status) { - return $trackingInfo->getCarrier(); + return $trackingInfo->getCarrier() ?? ''; } } } diff --git a/Plugin/Checkout/Block/LayoutProcessorPlugin.php b/Plugin/Checkout/Block/LayoutProcessorPlugin.php index 0bf99b1274d015257d0d9408263232671c1dfa73..57f3d5a7f27b8093c3d16e68502bf4984df92f23 100644 --- a/Plugin/Checkout/Block/LayoutProcessorPlugin.php +++ b/Plugin/Checkout/Block/LayoutProcessorPlugin.php @@ -1,4 +1,5 @@ <?php + namespace BobGroup\BobGo\Plugin\Checkout\Block; use Magento\Checkout\Block\Checkout\LayoutProcessor; @@ -18,42 +19,53 @@ class LayoutProcessorPlugin * Modify checkout layout to add suburb field. * * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject - * @param array $jsLayout - * @return array + * @param array<string, mixed> $jsLayout + * @return array<string, mixed> */ public function afterProcess( \Magento\Checkout\Block\Checkout\LayoutProcessor $subject, array $jsLayout - ) { + ): array { $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' => '' // Default value for the attribute - ]; - $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] - ['shippingAddress']['children']['shipping-address-fieldset']['children'][$suburbAttribute] - = $suburb; + if (isset($jsLayout['components']) && is_array($jsLayout['components']) + && isset($jsLayout['components']['checkout']) && is_array($jsLayout['components']['checkout']) + && isset($jsLayout['components']['checkout']['children']) && is_array($jsLayout['components']['checkout']['children']) + && isset($jsLayout['components']['checkout']['children']['steps']) && is_array($jsLayout['components']['checkout']['children']['steps']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']) + && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']) && is_array($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'])) { + + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] + ['shippingAddress']['children']['shipping-address-fieldset']['children'][$suburbAttribute] = [ + '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' => '' // Default value for the attribute + ]; + } return $jsLayout; } diff --git a/composer.json b/composer.json index 63303ed1e96ff42309d1791cb8dc3830aee2296d..f05cfbd12d9de0779d2879593c89c8cca5c49f84 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "OSL-3.0" ], "require": { - "php": "^7.4 || ^8.0 || ^8.1" + "php": "^7.4 || ^8.0 || ^8.2" }, "autoload": {