Files
stemcell/app/code/Magezon/QuickOrder/Controller/Index/LoadChildrens.php
T
2026-09-15 10:26:34 +08:00

110 lines
4.0 KiB
PHP
Executable File

<?php
/**
* Magezon
*
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to https://www.magezon.com for more information.
*
* @category Magezon
* @package Magezon_QuickOrder
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
*/
namespace Magezon\QuickOrder\Controller\Index;
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
class LoadChildrens extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var HelperFactory
*/
protected $helperFactory;
/**
* @var \Magezon\QuickOrder\Model\ProductManagement
*/
protected $productManagement;
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param HelperFactory $helperFactory
* @param \Magezon\QuickOrder\Model\ProductManagement $productManagement
* @param \Magento\Catalog\Model\ProductFactory $productFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
HelperFactory $helperFactory,
\Magezon\QuickOrder\Model\ProductManagement $productManagement,
\Magento\Catalog\Model\ProductFactory $productFactory
) {
parent::__construct($context);
$this->scopeConfig = $scopeConfig;
$this->helperFactory = $helperFactory;
$this->productManagement = $productManagement;
$this->productFactory = $productFactory;
}
private function usedGroupedImage()
{
return $this->scopeConfig->getValue(
\Magento\GroupedProduct\Block\Cart\Item\Renderer\Grouped::CONFIG_THUMBNAIL_SOURCE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* @return \Magento\Framework\Controller\Result\Raw
*/
public function execute()
{
$result['status'] = false;
try {
$post = $this->getRequest()->getPostValue();
if (isset($post['type']) && isset($post['id'])) {
$childrens = [];
$product = $this->productFactory->create();
$product->load($post['id']);
$helper = $this->helperFactory->create()->init($product, 'product_thumbnail_image');
switch ($post['type']) {
case 'grouped':
$associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
foreach ($associatedProducts as $product) {
$item = $this->productManagement->getProductData($product);
if ($this->usedGroupedImage()) {
$item['img'] = $helper->getUrl();
}
$childrens[] = $item;
}
break;
}
$result['childrens'] = $childrens;
$result['status'] = true;
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$result['message'] = $e->getMessage();
} catch (\Exception $e) {
$result['message'] = __('An error occurred while processing your request. Please try again later.');
}
$this->getResponse()->representJson(
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
);
return;
}
}