This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type as ProductType;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_text_swatch_attribute.php');
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('text_swatch_attribute');
$options = $attribute->getOptions();
/** @var WebsiteRepositoryInterface $websiteRepository */
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
$baseWebsite = $websiteRepository->get('base');
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
/** @var ProductFactory $productFactory */
$productFactory = $objectManager->get(ProductFactory::class);
$attributeValues = [];
$associatedProductIds = [];
$rootCategoryId = $baseWebsite->getDefaultStore()->getRootCategoryId();
array_shift($options);
foreach ($options as $option) {
$product = $productFactory->create();
$product->setTypeId(ProductType::TYPE_SIMPLE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Option ' . $option->getLabel())
->setSku(strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel())))
->setPrice(150)
->setTextSwatchAttribute($option->getValue())
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product);
$attributeValues[] = [
'label' => 'test',
'attribute_id' => $attribute->getId(),
'value_index' => $option->getValue(),
];
$associatedProductIds[] = $product->getId();
}
/** @var Factory $optionsFactory */
$optionsFactory = $objectManager->get(Factory::class);
$configurableAttributesData = [
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attributeValues,
],
];
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$product = $productFactory->create();
/** @var ProductExtensionFactory $extensionAttributesFactory */
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$product->setTypeId(Configurable::TYPE_CODE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Product')
->setSku('configurable')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($product);
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('text_swatch_attribute');
$options = $attribute->getOptions();
array_shift($options);
$productsArray = [];
foreach ($options as $option) {
$productsArray [] = strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel()));
}
$productsArray[] = 'configurable';
foreach ($productsArray as $sku) {
try {
$productRepository->deleteById($sku);
} catch (NoSuchEntityException $e) {
//Product already removed
}
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_text_swatch_attribute_rollback.php');
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type as ProductType;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Setup\CategorySetup;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_text_swatch_attribute.php');
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_visual_swatch_attribute.php');
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
$installer = $objectManager->create(CategorySetup::class);
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('text_swatch_attribute');
$secondAttribute = $productAttributeRepository->get('visual_swatch_attribute');
$options = $attribute->getOptions();
$secondAttributeOptions = $secondAttribute->getOptions();
/** @var WebsiteRepositoryInterface $websiteRepository */
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
$baseWebsite = $websiteRepository->get('base');
/** @var ProductAttributeRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
$attributeValues = [];
$secondAttributeValues = [];
$associatedProductIds = [];
$associatedProductIdsViaSecondAttribute = [];
$attributeSetId = $installer->getAttributeSetId(Product::ENTITY, 'Default');
$productFactory = $objectManager->get(ProductFactory::class);
$rootCategoryId = $baseWebsite->getDefaultStore()->getRootCategoryId();
array_shift($options);
array_shift($secondAttributeOptions);
foreach ($options as $option) {
foreach ($secondAttributeOptions as $secondAttrOption) {
$product = $productFactory->create();
$product->setTypeId(ProductType::TYPE_SIMPLE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Option ' . $option->getLabel())
->setSku(
strtolower(
str_replace(' ', '_', 'simple ' . $option->getLabel() . '_' . $secondAttrOption->getLabel())
)
)
->setPrice(150)
->setTextSwatchAttribute($option->getValue())
->setVisualSwatchAttribute($secondAttrOption->getValue())
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product, true);
$associatedProductIds[] = $product->getId();
}
$attributeValues[] = [
'label' => 'test1',
'attribute_id' => $attribute->getId(),
'value_index' => $option->getValue(),
];
}
foreach ($secondAttributeOptions as $secondAttrOption) {
$secondAttributeValues[] = [
'label' => 'test2',
'attribute_id' => $secondAttribute->getId(),
'value_index' => $secondAttrOption->getValue(),
];
}
$allAttributes = [$attribute, $secondAttribute];
$optionsFactory = $objectManager->get(Factory::class);
foreach ($allAttributes as $attribute) {
$configurableAttributesData[] =
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attribute->getAttributeCode() === 'text_swatch_attribute'
? $attributeValues
: $secondAttributeValues,
];
}
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$product = $productFactory->create();
/** @var ProductExtensionFactory $extensionAttributesFactory */
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$product->setTypeId(Configurable::TYPE_CODE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Product')
->setSku('configurable')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($product);
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$options = $productAttributeRepository->get('text_swatch_attribute')->getOptions();
$secondAttributeOptions = $productAttributeRepository->get('visual_swatch_attribute')->getOptions();
array_shift($options);
array_shift($secondAttributeOptions);
$productsArray = [];
foreach ($options as $option) {
foreach ($secondAttributeOptions as $secondAttrOption) {
$productsArray[] = strtolower(
str_replace(' ', '_', 'simple ' . $option->getLabel() . '_' . $secondAttrOption->getLabel())
);
}
}
$productsArray[] = 'configurable';
foreach ($productsArray as $sku) {
try {
$productRepository->deleteById($sku);
} catch (NoSuchEntityException $e) {
//Product already removed
}
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_text_swatch_attribute_rollback.php');
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_visual_swatch_attribute_rollback.php');
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type as ProductType;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_visual_swatch_attribute.php');
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('visual_swatch_attribute');
$options = $attribute->getOptions();
/** @var WebsiteRepositoryInterface $websiteRepository */
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
$baseWebsite = $websiteRepository->get('base');
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
/** @var ProductFactory $productFactory */
$productFactory = $objectManager->get(ProductFactory::class);
$attributeValues = [];
$associatedProductIds = [];
$rootCategoryId = $baseWebsite->getDefaultStore()->getRootCategoryId();
array_shift($options);
foreach ($options as $option) {
$product = $productFactory->create();
$product->setTypeId(ProductType::TYPE_SIMPLE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Option ' . $option->getLabel())
->setSku(strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel())))
->setPrice(150)
->setVisualSwatchAttribute($option->getValue())
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product);
$attributeValues[] = [
'label' => 'test',
'attribute_id' => $attribute->getId(),
'value_index' => $option->getValue(),
];
$associatedProductIds[] = $product->getId();
}
/** @var Factory $optionsFactory */
$optionsFactory = $objectManager->get(Factory::class);
$configurableAttributesData = [
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attributeValues,
],
];
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$product = $productFactory->create();
/** @var ProductExtensionFactory $extensionAttributesFactory */
$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class);
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$product->setTypeId(Configurable::TYPE_CODE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$baseWebsite->getId()])
->setName('Configurable Product')
->setSku('configurable')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([$rootCategoryId])
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($product);
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('visual_swatch_attribute');
$options = $attribute->getOptions();
array_shift($options);
$productsArray = [];
foreach ($options as $option) {
$productsArray [] = strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel()));
}
$productsArray[] = 'configurable';
foreach ($productsArray as $sku) {
try {
$productRepository->deleteById($sku);
} catch (NoSuchEntityException $e) {
//Product already removed
}
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/product_visual_swatch_attribute_rollback.php');
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture(
'Magento/Catalog/_files/product_image.php'
);
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/configurable_product_visual_swatch_attribute.php'
);
$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$configurableProduct = $productRepository->get('configurable');
$children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
$images = ['magento_image.jpg', 'magento_small_image.jpg', 'magento_thumbnail.jpg'];
foreach ($children as $index => $product) {
$product->setImage('/m/a/' . $images[$index])
->setSmallImage('/m/a/' . $images[$index])
->setThumbnail('/m/a/' . $images[$index])
->setData('media_gallery', ['images' => [
[
'file' => '/m/a/' . $images[$index],
'position' => 1,
'label' => 'Image Alt Text',
'disabled' => 0,
'media_type' => 'image',
],
]])
->setCanSaveCustomOptions(true)
->save();
}
@@ -0,0 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/configurable_product_visual_swatch_attribute_rollback.php'
);
Resolver::getInstance()->requireDataFixture(
'Magento/Catalog/_files/product_image_rollback.php'
);
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/visual_swatch_attribute_with_different_options_type.php'
);
$objectManager = Bootstrap::getObjectManager();
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
$attribute = $productAttributeRepository->get('test_configurable');
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductFactory $productFactory */
$productFactory = $objectManager->get(ProductFactory::class);
/** @var Factory $optionsFactory */
$optionsFactory = $objectManager->get(Factory::class);
/** @var ProductExtensionInterfaceFactory $productExtensionAttributes */
$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class);
/** @var WebsiteRepositoryInterface $websiteRepository */
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
$defaultWebsiteId = $websiteRepository->get('base')->getId();
$attributeValues = $associatedProductIds = [];
$options = $attribute->getSource()->getAllOptions();
array_shift($options);
foreach ($options as $option) {
/** @var Product $product */
$product = $productFactory->create();
$product->setTypeId(Type::TYPE_SIMPLE)
->setAttributeSetId($product->getDefaultAttributeSetId())
->setWebsiteIds([$defaultWebsiteId])
->setName('Configurable Option' . $option['label'])
->setSku('simple_' . str_replace(' ', '_', $option['label']))
->setPrice(100)
->setData('test_configurable', $option['value'])
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
$product = $productRepository->save($product);
$attributeValues[] = [
'label' => 'test',
'attribute_id' => $attribute->getId(),
'value_index' => $option['value'],
];
$associatedProductIds[] = $product->getId();
}
/** @var Product $configurableProduct */
$configurableProduct = $productFactory->create();
$configurableOptions = $optionsFactory->create(
[
[
'attribute_id' => $attribute->getId(),
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel(),
'position' => '0',
'values' => $attributeValues,
],
]
);
$extensionConfigurableAttributes = $configurableProduct->getExtensionAttributes()
?: $productExtensionAttributesFactory->create();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$configurableProduct->setExtensionAttributes($extensionConfigurableAttributes);
$configurableProduct->setTypeId(Configurable::TYPE_CODE)
->setAttributeSetId($configurableProduct->getDefaultAttributeSetId())
->setWebsiteIds([$defaultWebsiteId])
->setName('Configurable Product')
->setSku('configurable')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
$productRepository->save($configurableProduct);
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductAttributeRepositoryInterface $attributeRepository */
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$attribute = $attributeRepository->get('test_configurable');
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
$options = $attribute->getSource()->getAllOptions();
array_shift($options);
foreach ($options as $option) {
try {
$productRepository->deleteById('simple_' . str_replace(' ', '_', $option['label']));
} catch (NoSuchEntityException $e) {
//Product already removed
}
}
try {
$productRepository->deleteById('configurable');
} catch (NoSuchEntityException $e) {
//Product already removed
}
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/visual_swatch_attribute_with_different_options_type_rollback.php'
);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Catalog\Setup\CategorySetup;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/swatch_attribute.php');
$objectManager = Bootstrap::getObjectManager();
$installer = $objectManager->create(CategorySetup::class);
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$entityType = $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE);
$attribute = $objectManager->create(AttributeFactory::class)->create()
->loadByCode($entityType, 'color_swatch');
if ($attribute->getAttributeId()) {
/* Assign attribute to attribute set */
$installer->addAttributeToGroup(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'Default',
'General',
$attribute->getAttributeId()
);
}
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture('Magento/Swatches/_files/swatch_attribute_rollback.php');
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Catalog\Setup\CategorySetup;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
$installer = $objectManager->create(CategorySetup::class);
$attribute = $objectManager->create(AttributeFactory::class)->create();
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$entityType = $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE);
if (!$attribute->loadByCode($entityType, 'text_swatch_attribute')->getAttributeId()) {
$attribute->setData(
[
'frontend_label' => ['Text swatch attribute'],
'entity_type_id' => $entityType,
'frontend_input' => 'select',
'backend_type' => 'int',
'is_required' => '0',
'attribute_code' => 'text_swatch_attribute',
'is_global' => '1',
'is_user_defined' => 1,
'is_unique' => '0',
'is_searchable' => '0',
'is_comparable' => '0',
'is_filterable' => '1',
'is_filterable_in_search' => '0',
'is_used_for_promo_rules' => '0',
'is_html_allowed_on_front' => '1',
'used_in_product_listing' => '1',
'used_for_sort_by' => '0',
'swatch_input_type' => 'text',
'optiontext' => [
'order' => [
'option_0' => 1,
'option_1' => 2,
'option_3' => 3,
],
'value' => [
'option_0' => ['Option 1'],
'option_1' => ['Option 2'],
'option_2' => ['Option 3'],
],
],
'swatchtext' => [
'value' => [
'option_0' => ['Swatch 1'],
'option_1' => ['Swatch 2'],
'option_2' => ['Swatch 3'],
],
],
]
);
$attribute->save();
$installer->addAttributeToGroup(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'Default',
'General',
$attribute->getId()
);
}
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Registry;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
$objectManager = Bootstrap::getObjectManager();
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductAttributeRepositoryInterface $attributeRepository */
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
try {
$attributeRepository->deleteById('text_swatch_attribute');
} catch (NoSuchEntityException $e) {
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Catalog\Setup\CategorySetup;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
$installer = $objectManager->create(CategorySetup::class);
$attribute = $objectManager->create(AttributeFactory::class)->create();
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
$entityType = $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE);
if (!$attribute->loadByCode($entityType, 'visual_swatch_attribute')->getAttributeId()) {
$attribute->setData(
[
'frontend_label' => ['Visual swatch attribute'],
'entity_type_id' => $entityType,
'frontend_input' => 'select',
'backend_type' => 'int',
'is_required' => '0',
'attribute_code' => 'visual_swatch_attribute',
'is_global' => '1',
'is_user_defined' => 1,
'is_unique' => '0',
'is_searchable' => '0',
'is_comparable' => '0',
'is_filterable' => '1',
'is_filterable_in_search' => '0',
'is_used_for_promo_rules' => '0',
'is_html_allowed_on_front' => '1',
'used_in_product_listing' => '1',
'used_for_sort_by' => '0',
'swatch_input_type' => 'visual',
'swatchvisual' => [
'value' => [
'option_1' => '#555555',
'option_2' => '#aaaaaa',
'option_3' => '#ffffff',
],
],
'optionvisual' => [
'value' => [
'option_1' => ['option 1'],
'option_2' => ['option 2'],
'option_3' => ['option 3']
],
],
'options' => [
'option' => [
['label' => 'Option 1', 'value' => 'option_1'],
['label' => 'Option 2', 'value' => 'option_2'],
['label' => 'Option 3', 'value' => 'option_3'],
],
],
]
);
$attributeRepository->save($attribute);
$installer->addAttributeToGroup(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'Default',
'General',
$attribute->getId()
);
}
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Registry;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductAttributeRepositoryInterface $attributeRepository */
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
try {
$attributeRepository->deleteById('visual_swatch_attribute');
} catch (NoSuchEntityException $e) {
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,208 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Eav\Api\Data\AttributeOptionInterface;
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
$data = [
'attribute_code' => 'color_swatch',
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
'is_global' => 1,
'is_user_defined' => 1,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 1,
'is_visible_in_advanced_search' => 1,
'is_comparable' => 1,
'is_filterable' => 1,
'is_filterable_in_search' => 1,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 1,
'used_in_product_listing' => 1,
'used_for_sort_by' => 1,
'frontend_label' => ['Test Swatch'],
'backend_type' => 'int',
'use_product_image_for_swatch' => 0,
'update_product_preview_image' => 0,
];
$optionsPerAttribute = 3;
$data['swatch_input_type'] = 'visual';
$data['swatchvisual']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = '#'
. str_repeat(
dechex(255 * $index / $optionsPerAttribute),
3
);
return $values;
},
[]
);
$data['optionvisual']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$data['options']['option'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values[] = [
'label' => 'option ' . $index,
'value' => 'option_' . $index,
];
return $values;
},
[]
);
$options = [];
foreach ($data['options']['option'] as $optionData) {
$options[] = $objectManager->get(AttributeOptionInterface::class)
->setLabel($optionData['label'])
->setValue($optionData['value']);
}
$attribute = $objectManager->create(
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
['data' => $data]
);
$attribute->setOptions($options);
$attribute->save();
/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
/** @var \Magento\Eav\Model\Config $eavConfig */
$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
$eavConfig->clear();
$attribute = $eavConfig->getAttribute('catalog_product', 'color_swatch');
$options = $attribute->getOptions();
// workaround for saved attribute
$attribute->setDefaultValue($options[1]->getValue());
$attribute->save();
$eavConfig->clear();
/** @var $product \Magento\Catalog\Model\Product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(10)
->setAttributeSetId(4)
->setName('Simple Product1')
->setSku('simple1')
->setTaxClassId('none')
->setDescription('description')
->setShortDescription('short description')
->setOptionsContainer('container1')
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
->setPrice(10)
->setWeight(1)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCategoryIds([])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
->setSpecialPrice('5.99')
->save();
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(11)
->setAttributeSetId(4)
->setName('Simple Product2')
->setSku('simple2')
->setTaxClassId('none')
->setDescription('description')
->setShortDescription('short description')
->setOptionsContainer('container1')
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
->setPrice(20)
->setWeight(1)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCategoryIds([])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
->setSpecialPrice('15.99')
->save();
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(12)
->setAttributeSetId(4)
->setName('Simple Product3')
->setSku('simple3')
->setTaxClassId('none')
->setDescription('description')
->setShortDescription('short description')
->setPrice(30)
->setWeight(1)
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCategoryIds([])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
->setSpecialPrice('25.99')
->save();
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
$category->isObjectNew(true);
$category->setId(
333
)->setCreatedAt(
'2014-06-23 09:50:07'
)->setName(
'Category 1'
)->setParentId(
2
)->setPath(
'1/2/333'
)->setLevel(
2
)->setAvailableSortBy(
['position', 'name']
)->setDefaultSortBy(
'name'
)->setIsActive(
true
)->setPosition(
1
)->setPostedProducts(
[10 => 10, 11 => 11, 12 => 12]
)->save();
/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */
$indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
$indexerCollection->load();
/** @var \Magento\Indexer\Model\Indexer $indexer */
foreach ($indexerCollection->getItems() as $indexer) {
$indexer->reindexAll();
}
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
foreach (['simple1', 'simple2', 'simple3'] as $sku) {
try {
$product = $productRepository->get($sku, false, null, true);
$productRepository->delete($product);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
//Product already removed
}
}
$productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
foreach ($productCollection as $product) {
$product->delete();
}
/** @var $category \Magento\Catalog\Model\Category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
$category->load(333);
if ($category->getId()) {
$category->delete();
}
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
$attribute->loadByCode(4, 'color_swatch');
if ($attribute->getId()) {
$attribute->delete();
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Eav\Api\Data\AttributeOptionInterface;
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$data = [
'is_required' => 1,
'is_visible_on_front' => 1,
'is_visible_in_advanced_search' => 0,
'attribute_code' => 'color_swatch',
'backend_type' => '',
'is_searchable' => 0,
'is_filterable' => 0,
'is_user_defined' => 1,
'is_filterable_in_search' => 0,
'frontend_label' => 'Attribute ',
'entity_type_id' => 4
];
$optionsPerAttribute = 3;
$data['frontend_input'] = 'swatch_visual';
$data['swatch_input_type'] = 'visual';
$data['swatchvisual']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = '#'
. str_repeat(
dechex(255 * $index / $optionsPerAttribute),
3
);
return $values;
},
[]
);
$data['optionvisual']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$data['options']['option'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values[] = [
'label' => 'option ' . $index,
'value' => 'option_' . $index,
];
return $values;
},
[]
);
$options = [];
foreach ($data['options']['option'] as $optionData) {
$options[] = $objectManager->get(AttributeOptionInterface::class)
->setLabel($optionData['label'])
->setValue($optionData['value']);
}
$attribute = $objectManager->create(
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
['data' => $data]
);
$attribute->setOptions($options);
$attribute->save();
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
$attribute->loadByCode(4, 'color_swatch');
if ($attribute->getId()) {
$attribute->delete();
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Setup\CategorySetup;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Swatches\Model\Swatch;
use Magento\TestFramework\Helper\Bootstrap;
/** @var $installer CategorySetup */
$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
/** @var AttributeRepositoryInterface $attributeRepository */
$attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
// Add attribute data
$data = [
'attribute_code' => 'test_configurable',
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
'is_global' => 1,
'is_user_defined' => 1,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 0,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Test Configurable'],
'backend_type' => 'int',
];
$optionsPerAttribute = 3;
$data['frontend_input'] = 'select';
$data['swatch_input_type'] = Swatch::SWATCH_INPUT_TYPE_TEXT;
$data['swatchtext']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$data['optiontext']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$data['optiontext']['order'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = $index;
return $values;
},
[]
);
$data['options']['option'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values[] = [
'label' => 'option ' . $index,
'value' => 'option_' . $index,
];
return $values;
},
[]
);
$options = [];
foreach ($data['options']['option'] as $optionData) {
$options[] = Bootstrap::getObjectManager()->create(AttributeOptionInterface::class)
->setLabel($optionData['label'])
->setValue($optionData['value']);
}
$attribute = Bootstrap::getObjectManager()->create(
ProductAttributeInterface::class,
['data' => $data]
);
$attribute->setOptions($options);
$attributeRepository->save($attribute);
/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
$attribute->loadByCode(4, 'test_configurable');
if ($attribute->getId()) {
$attribute->delete();
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Setup\CategorySetup;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Setup\Fixtures\ImagesGenerator\ImagesGenerator;
use Magento\Swatches\Helper\Media as SwatchesMedia;
use Magento\Swatches\Model\Swatch;
use Magento\TestFramework\Helper\Bootstrap;
/** @var $installer CategorySetup */
$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
/** @var AttributeRepositoryInterface $attributeRepository */
$attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
// Generate swatch image
/** @var ImagesGenerator $imagesGenerator */
$imagesGenerator = Bootstrap::getObjectManager()->get(ImagesGenerator::class);
/** @var SwatchesMedia $swatchesMedia */
$swatchesMedia = Bootstrap::getObjectManager()->get(SwatchesMedia::class);
$imageName = '/visual_swatch_attribute_option_type_image.jpg';
$imagesGenerator->generate([
'image-width' => 110,
'image-height' => 90,
'image-name' => $imageName,
]);
$imagePath = substr($swatchesMedia->moveImageFromTmp($imageName), 1);
$swatchesMedia->generateSwatchVariations($imagePath);
// Add attribute data
$data = [
'attribute_code' => 'test_configurable',
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
'is_global' => 1,
'is_user_defined' => 1,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 0,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Test Configurable'],
'backend_type' => 'int',
];
$optionsPerAttribute = 3;
$data['frontend_input'] = 'select';
$data['swatch_input_type'] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
$data['swatchvisual']['value'] = [
'option_1' => '#000000', // HEX color (color type)
'option_2' => $imagePath, // image path (image type)
'option_3' => null, // null (empty type)
];
$data['optionvisual']['value'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$data['optionvisual']['order'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values['option_' . $index] = $index;
return $values;
},
[]
);
$data['options']['option'] = array_reduce(
range(1, $optionsPerAttribute),
function ($values, $index) use ($optionsPerAttribute) {
$values[] = [
'label' => 'option ' . $index,
'value' => 'option_' . $index,
];
return $values;
},
[]
);
$options = [];
foreach ($data['options']['option'] as $optionData) {
$options[] = Bootstrap::getObjectManager()->create(AttributeOptionInterface::class)
->setLabel($optionData['label'])
->setValue($optionData['value']);
}
$attribute = Bootstrap::getObjectManager()->create(
ProductAttributeInterface::class,
['data' => $data]
);
$attribute->setOptions($options);
$attributeRepository->save($attribute);
/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Swatches\Helper\Media as SwatchesMedia;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
/** @var ProductAttributeRepositoryInterface $attributeRepository */
$attributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
try {
$attribute = $attributeRepository->get('test_configurable');
$attributeRepository->delete($attribute);
} catch (NoSuchEntityException $exception) {
//Product already removed
}
/** @var WriteInterface $mediaDirectory */
$mediaDirectory = $objectManager->get(Filesystem::class)
->getDirectoryWrite(
DirectoryList::MEDIA
);
/** @var SwatchesMedia $swatchesMedia */
$swatchesMedia = $objectManager->get(SwatchesMedia::class);
$testImageName = 'visual_swatch_attribute_option_type_image.jpg';
$testImageSwatchPath = $swatchesMedia->getAttributeSwatchPath($testImageName);
$mediaDirectory->delete($testImageSwatchPath);
$imageConfig = $swatchesMedia->getImageConfig();
$swatchTypes = ['swatch_image', 'swatch_thumb'];
foreach ($swatchTypes as $swatchType) {
$absolutePath = $mediaDirectory->getAbsolutePath($swatchesMedia->getSwatchCachePath($swatchType));
$swatchTypePath = $absolutePath . $swatchesMedia->getFolderNameSize($swatchType, $imageConfig) .
'/' . $testImageName;
$mediaDirectory->delete($swatchTypePath);
}
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Model\Product;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/visual_swatch_attribute_with_different_options_type.php'
);
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_products.php');
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_image.php');
// set 'Product Image for Swatch' for attribute
$attribute->setData('use_product_image_for_swatch', 1);
$attributeRepository->save($attribute);
// get first child and set image
$childrenProducts = $product->getTypeInstance()->getUsedProducts($product);
/** @var Product $firstChildSimpleProduct */
$firstChildSimpleProduct = array_shift($childrenProducts);
$firstChildSimpleProduct
->setImage('/m/a/magento_image.jpg')
->setSmallImage('/m/a/magento_image.jpg')
->setThumbnail('/m/a/magento_image.jpg')
->setData('media_gallery', ['images' => [
[
'file' => '/m/a/magento_image.jpg',
'position' => 1,
'label' => 'Image Alt Text',
'disabled' => 0,
'media_type' => 'image'
],
]])
->save();
@@ -0,0 +1,14 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
Resolver::getInstance()->requireDataFixture(
'Magento/Swatches/_files/visual_swatch_attribute_with_different_options_type_rollback.php'
);
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_products_rollback.php');
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_image_rollback.php');