This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Esun\ProductInquiry\Block\Adminhtml\Inquiry;
use Esun\ProductInquiry\Model\Inquiry;
use Magento\Backend\Block\Template;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class View extends Template
{
public function __construct(
Template\Context $context,
private readonly Registry $registry,
private readonly Json $json,
private readonly TimezoneInterface $timezone,
array $data = []
) {
parent::__construct($context, $data);
}
public function getInquiry(): Inquiry
{
return $this->registry->registry('current_product_inquiry');
}
public function getSelectedOptions(): array
{
try {
return (array)$this->json->unserialize((string)$this->getInquiry()->getData('selected_options'));
} catch (\InvalidArgumentException) {
return [];
}
}
public function getStatusOptions(): array
{
return Inquiry::getStatuses();
}
public function getAvailabilityLabel(): string
{
return match ((string)$this->getInquiry()->getData('availability_reason')) {
'manual_out_of_stock' => (string)__('Out of Stock'),
'msi_not_salable' => (string)__('MSI Not Salable'),
'both' => (string)__('Out of Stock and MSI Not Salable'),
default => (string)__('Unknown'),
};
}
public function getFormattedCreatedAt(): string
{
$timezoneName = (string)$this->timezone->getConfigTimezone() ?: 'UTC';
$createdAt = new \DateTimeImmutable(
(string)$this->getInquiry()->getData('created_at'),
new \DateTimeZone('UTC')
);
$localCreatedAt = $createdAt->setTimezone(new \DateTimeZone($timezoneName));
$formattedDate = $this->timezone->formatDateTime(
$createdAt,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
null,
$timezoneName
);
return sprintf('%s (%s, UTC%s)', $formattedDate, $timezoneName, $localCreatedAt->format('P'));
}
}
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Esun\ProductInquiry\Block;
use Esun\ProductInquiry\Model\AvailabilityResolver;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\StoreManagerInterface;
class ProductInquiry extends Template
{
public function __construct(
Template\Context $context,
private readonly Registry $registry,
private readonly StoreManagerInterface $storeManager,
private readonly CountryCollectionFactory $countryCollectionFactory,
private readonly AvailabilityResolver $availabilityResolver,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
}
public function getProduct(): ?Product
{
$product = $this->registry->registry('current_product');
return $product instanceof Product ? $product : null;
}
public function canRender(): bool
{
$product = $this->getProduct();
return $product && $product->getTypeId() === Configurable::TYPE_CODE;
}
public function getCountryOptions(): array
{
return $this->countryCollectionFactory->create()->loadByStore()->toOptionArray(false);
}
public function getJsonConfig(): string
{
$parent = $this->getProduct();
if (!$parent || !$this->canRender()) {
return '{}';
}
$children = [];
$website = $this->storeManager->getStore()->getWebsite();
foreach ($parent->getTypeInstance()->getUsedProducts($parent) as $child) {
if ((int)$child->getStatus() !== Status::STATUS_ENABLED) {
continue;
}
$availability = $this->availabilityResolver->resolve($child, $website);
$optionLabels = [];
foreach ($parent->getTypeInstance()->getConfigurableAttributes($parent) as $configurableAttribute) {
$attribute = $configurableAttribute->getProductAttribute();
$text = $child->getAttributeText((string)$attribute->getAttributeCode());
$optionLabels[] = [
'label' => (string)$attribute->getStoreLabel(),
'value' => is_array($text) ? implode(', ', $text) : (string)$text,
];
}
$children[(string)$child->getId()] = [
'id' => (int)$child->getId(),
'sku' => (string)$child->getSku(),
'name' => (string)$child->getName(),
'options' => $optionLabels,
'requiresInquiry' => (bool)$availability['requires_inquiry'],
'availabilityReason' => (string)$availability['reason'],
];
}
return $this->json->serialize([
'parentId' => (int)$parent->getId(),
'parentName' => (string)$parent->getName(),
'children' => $children,
'submitUrl' => $this->getUrl('product-inquiry/index/post'),
]);
}
}