Files
module-vendor-notes/Block/Adminhtml/Order/View/VendorNotes.php

113 lines
3.2 KiB
PHP
Raw Normal View History

2025-10-07 18:31:07 -04:00
<?php
namespace Shopkeeper\VendorNotes\Block\Adminhtml\Order\View;
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;
2025-10-07 18:31:07 -04:00
class VendorNotes extends Template
{
protected $_order;
protected $_scopeConfig;
protected $json;
2025-10-07 18:31:07 -04:00
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Sales\Model\Order $order,
ScopeConfigInterface $scopeConfig,
Json $json,
2025-10-07 18:31:07 -04:00
array $data = []
) {
$this->_order = $order;
$this->_scopeConfig = $scopeConfig;
$this->json = $json;
2025-10-07 18:31:07 -04:00
parent::__construct($context, $data);
}
public function getOrder()
{
return $this->_order->load($this->getRequest()->getParam('order_id'));
}
public function getVendorNotes()
{
$order = $this->getOrder();
$notes = [];
// Get the vendor mapping configuration
$vendorMapping = $this->getVendorMapping();
if (empty($vendorMapping)) {
return $notes;
}
2025-10-07 18:31:07 -04:00
foreach ($order->getAllItems() as $item) {
$product = $item->getProduct();
$vendorId = $product->getData('vendor');
2025-10-07 18:31:07 -04:00
if ($vendorId) {
$vendorLabel = $product->getAttributeText('vendor');
// 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;
2025-10-07 18:31:07 -04:00
}
}
}
}
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 [];
}
}
2025-10-08 09:34:26 -04:00
/**
* Filter output to allow safe HTML tags
*
* @param string $content
* @return string
*/
public function filterOutputHtml($content)
{
// Decode HTML entities first in case the content was double-encoded
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
// Use Magento's filter to allow specific HTML tags
// This is safer than just echoing raw HTML
return $this->filterTemplate->filter($content);
}
}