From c691c4fbe71c3686cad2bd660705bd4a06df86b0 Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Thu, 15 Aug 2024 13:08:12 +0200 Subject: [PATCH] add the weight unit to the orders api response --- Plugin/AddWeightUnitToOrderPlugin.php | 40 +++++++++++++++++++++++++++ Setup/InstallData.php | 34 +++++++++++++++++++++++ etc/di.xml | 6 ++++ etc/events.xml | 3 ++ etc/frontend/di.xml | 1 - 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Plugin/AddWeightUnitToOrderPlugin.php create mode 100644 Setup/InstallData.php create mode 100644 etc/di.xml create mode 100644 etc/events.xml diff --git a/Plugin/AddWeightUnitToOrderPlugin.php b/Plugin/AddWeightUnitToOrderPlugin.php new file mode 100644 index 0000000..6beb3c8 --- /dev/null +++ b/Plugin/AddWeightUnitToOrderPlugin.php @@ -0,0 +1,40 @@ +<?php + +namespace BobGroup\BobGo\Plugin; + +use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\OrderRepositoryInterface; +use Psr\Log\LoggerInterface; +use Magento\Framework\App\Config\ScopeConfigInterface; + +class AddWeightUnitToOrderPlugin +{ + protected $logger; + protected $scopeConfig; + + public function __construct( + LoggerInterface $logger, + ScopeConfigInterface $scopeConfig + ) { + $this->logger = $logger; + $this->scopeConfig = $scopeConfig; + } + + public function beforeSave( + OrderRepositoryInterface $subject, + OrderInterface $order + ) { + $weightUnit = $this->scopeConfig->getValue( + 'general/locale/weight_unit', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + + if ($weightUnit) { + foreach ($order->getItems() as $orderItem) { + $orderItem->setData('product_type', $weightUnit); + } + } + + return [$order]; + } +} diff --git a/Setup/InstallData.php b/Setup/InstallData.php new file mode 100644 index 0000000..a8db474 --- /dev/null +++ b/Setup/InstallData.php @@ -0,0 +1,34 @@ +<?php + +namespace BobGroup\BobGo\Setup; + +use Magento\Framework\Setup\InstallDataInterface; +use Magento\Framework\Setup\ModuleContextInterface; +use Magento\Framework\Setup\ModuleDataSetupInterface; +use Magento\Eav\Setup\EavSetupFactory; + +class InstallData implements InstallDataInterface +{ + private $eavSetupFactory; + + public function __construct(EavSetupFactory $eavSetupFactory) + { + $this->eavSetupFactory = $eavSetupFactory; + } + + public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); + + $eavSetup->addAttribute( + \Magento\Sales\Model\Order::ENTITY, + 'weight_unit', + [ + 'type' => 'varchar', + 'length' => 255, + 'nullable' => true, + 'comment' => 'Weight Unit', + ] + ); + } +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..5d81287 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,6 @@ +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> + <type name="Magento\Sales\Api\OrderRepositoryInterface"> + <plugin name="add_weight_unit_to_order_item" type="BobGroup\BobGo\Plugin\AddWeightUnitToOrderPlugin" /> + </type> + +</config> diff --git a/etc/events.xml b/etc/events.xml new file mode 100644 index 0000000..bfb5e96 --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,3 @@ +<?xml version="1.0"?> +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> +</config> diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index f424156..bc1aaeb 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -13,5 +13,4 @@ <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument> </arguments> </type> - </config> -- GitLab