init
This commit is contained in:
@@ -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'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user