diff --git a/Plugin/AddWeightUnitToOrderPlugin.php b/Plugin/AddWeightUnitToOrderPlugin.php new file mode 100644 index 0000000000000000000000000000000000000000..6beb3c830db33003f982413eac77ce68dfa17206 --- /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 0000000000000000000000000000000000000000..a8db474706e1ad2e2ca504bd4480b3d74e2bc52f --- /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 0000000000000000000000000000000000000000..5d8128708181eb7593b6ab8ae4c4ae5c63923e73 --- /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 0000000000000000000000000000000000000000..bfb5e9669522f329879ecc96da2ca4a571ea8b2e --- /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 f424156d3030c7560e9df0dbd97a35cd3ea16493..bc1aaebb43890432e80d8e9d27bfd5838b7c8e8b 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>