From 06e38aa331ba767505d0532026fa55491fc32ba3 Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Thu, 3 Oct 2024 11:47:31 +0200 Subject: [PATCH] change shipping description --- Observer/ModifyShippingDescription.php | 70 ++++++++++++++++++++++++++ etc/events.xml | 5 ++ 2 files changed, 75 insertions(+) create mode 100644 Observer/ModifyShippingDescription.php diff --git a/Observer/ModifyShippingDescription.php b/Observer/ModifyShippingDescription.php new file mode 100644 index 0000000..3c19823 --- /dev/null +++ b/Observer/ModifyShippingDescription.php @@ -0,0 +1,70 @@ +<?php + +namespace BobGroup\BobGo\Observer; + +use Magento\Framework\Event\ObserverInterface; +use Magento\Framework\Event\Observer; +use Psr\Log\LoggerInterface; + +class ModifyShippingDescription implements ObserverInterface +{ + public const CODE = 'bobgo'; + + protected $logger; + + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + + public function execute(Observer $observer) + { + // Get the order object from the event + $order = $observer->getEvent()->getOrder(); + + // Get the current shipping description + $shippingDescription = $order->getShippingDescription(); + + // Log the original shipping description + $this->logger->info('Original Shipping Description: ' . $shippingDescription); + + // Get the shipping method used in the order (e.g., bobgo_10303_39_1) + $shippingMethod = $order->getShippingMethod(); + + // Get the method title from the shipping description (which might already include the title) + $methodTitle = $this->extractMethodTitle($shippingDescription); + + // Log the method title for debugging + $this->logger->info('Extracted Method Title: ' . $methodTitle); + + // Set the new dynamic shipping description based only on MethodTitle + $newDescription = $methodTitle; + + // Update the shipping description in the order + $order->setShippingDescription($newDescription); + + // Optionally log the new shipping description + $this->logger->info('Updated Shipping Description: ' . $newDescription); + } + + /** + * Helper function to extract the method title from the original shipping description + * + * @param string $shippingDescription + * @return string + */ + private function extractMethodTitle($shippingDescription) + { + // Find the position of the last dash in the string + $lastDashPosition = strrpos($shippingDescription, ' - '); + + // If a dash is found, extract the part after the last dash + if ($lastDashPosition !== false) { + return trim(substr($shippingDescription, $lastDashPosition + 3)); // +3 to skip the ' - ' part + } + + // If no dash is found, return the full description (fallback) + return $shippingDescription; + } + +} diff --git a/etc/events.xml b/etc/events.xml index d4d14f1..a6fab5c 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -3,4 +3,9 @@ <event name="sales_order_save_after"> <observer name="order_create_webhook" instance="BobGroup\BobGo\Observer\OrderCreateWebhook"/> </event> + <!-- app/code/Vendor/Module/etc/events.xml --> + <event name="sales_order_place_before"> + <observer name="modify_shipping_description" instance="BobGroup\BobGo\Observer\ModifyShippingDescription"/> + </event> + </config> -- GitLab