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;
|
2025-10-07 18:57:28 -04:00
|
|
|
use Magento\Framework\Serialize\Serializer\Json;
|
2025-10-07 19:03:16 -04:00
|
|
|
use Magento\Framework\Filter\Template as FilterTemplate;
|
2025-10-07 18:31:07 -04:00
|
|
|
|
|
|
|
|
class VendorNotes extends Template
|
|
|
|
|
{
|
|
|
|
|
protected $_order;
|
|
|
|
|
protected $_scopeConfig;
|
2025-10-07 18:57:28 -04:00
|
|
|
protected $json;
|
2025-10-07 19:03:16 -04:00
|
|
|
protected $filterTemplate;
|
2025-10-07 18:31:07 -04:00
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
\Magento\Backend\Block\Template\Context $context,
|
|
|
|
|
\Magento\Sales\Model\Order $order,
|
|
|
|
|
ScopeConfigInterface $scopeConfig,
|
2025-10-07 18:57:28 -04:00
|
|
|
Json $json,
|
2025-10-07 19:03:16 -04:00
|
|
|
FilterTemplate $filterTemplate,
|
2025-10-07 18:31:07 -04:00
|
|
|
array $data = []
|
|
|
|
|
) {
|
|
|
|
|
$this->_order = $order;
|
|
|
|
|
$this->_scopeConfig = $scopeConfig;
|
2025-10-07 18:57:28 -04:00
|
|
|
$this->json = $json;
|
2025-10-07 19:03:16 -04:00
|
|
|
$this->filterTemplate = $filterTemplate;
|
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 = [];
|
2025-10-07 18:57:28 -04:00
|
|
|
|
|
|
|
|
// 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:57:28 -04:00
|
|
|
|
2025-10-07 18:31:07 -04:00
|
|
|
if ($vendorId) {
|
|
|
|
|
$vendorLabel = $product->getAttributeText('vendor');
|
2025-10-07 18:57:28 -04:00
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2025-10-07 18:57:28 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|