86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Stemcell\Catalog\Helper;
|
|
|
|
use Magento\Catalog\Model\Product;
|
|
use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable;
|
|
use \Magento\ConfigurableProduct\Helper\Data as ConfigurableProductHelper;
|
|
|
|
class Configuration
|
|
{
|
|
public function __construct(
|
|
private Configurable $configurableBlock,
|
|
private ConfigurableProductHelper $configurableProductHelper,
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* get configurable attribute associated product
|
|
* @param Product $product
|
|
* @return array|mixed []
|
|
*/
|
|
public function getConfigurableAttributeProducts(Product $product) {
|
|
$this->configurableBlock->setProduct($product);
|
|
$options = $this->configurableProductHelper->getOptions($product, $this->configurableBlock->getAllowProducts());
|
|
return $options['salable'] ?? [];
|
|
}
|
|
|
|
protected function getProductAttributesIdByCode(Product $product, array $attributeCodes)
|
|
{
|
|
$attributesId = [];
|
|
foreach ($product->getAttributes() as $attribute) {
|
|
if (in_array($attribute->getAttributeCode(), $attributeCodes)) {
|
|
$attributesId[] = $attribute->getAttributeId();
|
|
}
|
|
}
|
|
return $attributesId;
|
|
}
|
|
|
|
public function extractContentBetweenPTag(string $content)
|
|
{
|
|
if ($content) {
|
|
preg_match('/<p>(.*?)<\/p>/', $content, $matches);
|
|
return isset($matches[1]) ? $matches[1] : $content;
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Get product images for configurable variations
|
|
* @param Product $configurableProduct
|
|
* @return array
|
|
*/
|
|
public function getOptionImages(Product $configurableProduct)
|
|
{
|
|
$images = [];
|
|
$shortDescription = [];
|
|
|
|
$allProducts = $configurableProduct->getTypeInstance()
|
|
->setStoreFilter($configurableProduct->getStoreId(), $configurableProduct)
|
|
->getUsedProducts($configurableProduct, null);
|
|
|
|
foreach ($allProducts as $product) {
|
|
/* @var $product Product */
|
|
$product->load($product->getId());
|
|
foreach ($product->getMediaGalleryImages() as $image) {
|
|
|
|
$images[$product->getId()][] =
|
|
[
|
|
'file' => $image->getFile(),
|
|
'label' => $image->getLabel(),
|
|
'position' => $image->getPosition(),
|
|
'types' => $image->getTypes(),
|
|
'url' => $product->getMediaConfig()->getMediaUrl($image->getFile())
|
|
];
|
|
}
|
|
|
|
$shortDescription[$product->getId()]['short_description'] =
|
|
$this->extractContentBetweenPTag($product->getData('feature'));
|
|
$shortDescription[$product->getId()]['sku'] = $product->getSku();
|
|
}
|
|
|
|
return [$images, $shortDescription];
|
|
}
|
|
}
|