Skip to content

Fix #29063 - Create configurations: not all configurable attributes are visible #30546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model;

use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Zend_Db_Expr;

class ConfigurableAttributeHandler
{
/**
* Attribute collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory
* @param CollectionFactory $attributeColFactory
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory
CollectionFactory $attributeColFactory
) {
$this->collectionFactory = $attributeColFactory;
}
Expand All @@ -32,28 +38,47 @@ public function getApplicableAttributes()
{
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */
$collection = $this->collectionFactory->create();
return $collection->addFieldToFilter(
$collection->addFieldToFilter(
'frontend_input',
'select'
)->addFieldToFilter(
'is_user_defined',
1
)->addFieldToFilter(
'is_global',
\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL
ScopedAttributeInterface::SCOPE_GLOBAL
);

$types = [
Type::TYPE_SIMPLE,
Type::TYPE_VIRTUAL,
Configurable::TYPE_CODE,
];
$applyToArr = [];
foreach ($types as $type) {
$applyToArr[] = "apply_to REGEXP '(^|(.*,))$type($|,.*)'";
}
$whereExprStr = 'apply_to IS NULL OR (' . implode(' AND ', $applyToArr) . ')';

$collection->getSelect()->where(new Zend_Db_Expr($whereExprStr));

return $collection;
}

/**
* Check if attribute is applicable for configurable products
* @deprecated is applicable check is added to collection query
* @see \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler::getApplicableAttributes
* @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
*
* @return bool
*/
public function isAttributeApplicable($attribute)
{
$types = [
\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE,
\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE,
Type::TYPE_SIMPLE,
Type::TYPE_VIRTUAL,
Configurable::TYPE_CODE,
];
return !$attribute->getApplyTo() || count(array_diff($types, $attribute->getApplyTo())) === 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ public function getSuggestedAttributes($labelPart)
$result = [];
foreach ($collection->getItems() as $id => $attribute) {
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
$result[$id] = [
'id' => $attribute->getId(),
'label' => $attribute->getFrontendLabel(),
'code' => $attribute->getAttributeCode(),
'options' => $attribute->getSource()->getAllOptions(false),
];
}
$result[$id] = [
'id' => $attribute->getId(),
'label' => $attribute->getFrontendLabel(),
'code' => $attribute->getAttributeCode(),
'options' => $attribute->getSource()->getAllOptions(false),
];
}

return $result;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,14 @@ public function getCollection()
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function getData()
{
$items = [];
$skippedItems = 0;
foreach ($this->getCollection()->getItems() as $attribute) {
if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
$items[] = $attribute->toArray();
} else {
$skippedItems++;
}
if (!$this->getCollection()->isLoaded()) {
$this->getCollection()->load();
}
return [
'totalRecords' => $this->collection->getSize() - $skippedItems,
'items' => $items
];

return $this->getCollection()->toArray();
}
}