Added ability to add/edit note fields in Magento Admin.

This commit is contained in:
shopkeeperdev
2025-10-07 18:57:28 -04:00
parent 44c5b511a3
commit fcc8697c75
5 changed files with 186 additions and 38 deletions

View File

@@ -5,20 +5,24 @@ use Magento\Backend\Block\Template;
use Magento\Sales\Model\Order;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Serialize\Serializer\Json;
class VendorNotes extends Template
{
protected $_order;
protected $_scopeConfig;
protected $json;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Sales\Model\Order $order,
ScopeConfigInterface $scopeConfig,
Json $json,
array $data = []
) {
$this->_order = $order;
$this->_scopeConfig = $scopeConfig;
$this->json = $json;
parent::__construct($context, $data);
}
@@ -31,22 +35,34 @@ class VendorNotes extends Template
{
$order = $this->getOrder();
$notes = [];
$vendorConfigPaths = [
'Thermo Fisher' => 'vendor_notes/general/thermo_fisher_note',
'Orion' => 'vendor_notes/general/orion_note',
'Nalgene' => 'vendor_notes/general/nalgene_note',
'Thermo Fisher Parts' => 'vendor_notes/general/thermo_fisher_parts_note'
];
// Get the vendor mapping configuration
$vendorMapping = $this->getVendorMapping();
if (empty($vendorMapping)) {
return $notes;
}
foreach ($order->getAllItems() as $item) {
$product = $item->getProduct();
$vendorId = $product->getData('vendor');
if ($vendorId) {
$vendorLabel = $product->getAttributeText('vendor');
if (isset($vendorConfigPaths[$vendorLabel])) {
$note = $this->_scopeConfig->getValue($vendorConfigPaths[$vendorLabel], ScopeInterface::SCOPE_STORE);
if ($note && !in_array($note, $notes)) {
$notes[] = $note;
// Look for matching vendor in the mapping
foreach ($vendorMapping as $mapping) {
if (isset($mapping['vendor_name']) &&
isset($mapping['note']) &&
$mapping['vendor_name'] === $vendorLabel) {
$note = $mapping['note'];
// Avoid duplicate notes
if ($note && !in_array($note, $notes)) {
$notes[] = $note;
}
break;
}
}
}
@@ -54,4 +70,28 @@ class VendorNotes extends Template
return $notes;
}
}
/**
* Get vendor mapping from configuration
*
* @return array
*/
protected function getVendorMapping()
{
$config = $this->_scopeConfig->getValue(
'vendor_notes/general/vendor_mapping',
ScopeInterface::SCOPE_STORE
);
if (!$config) {
return [];
}
try {
$mapping = $this->json->unserialize($config);
return is_array($mapping) ? $mapping : [];
} catch (\Exception $e) {
return [];
}
}
}