|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 9 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 12 | +use Magento\Store\Model\Store; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use Magento\TestFramework\Catalog\Model\GetCategoryByName; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\Workaround\Override\Fixture\Resolver; |
| 17 | + |
| 18 | +Resolver::getInstance()->requireDataFixture( |
| 19 | + 'Magento/Catalog/_files/products_with_layered_navigation_attribute.php' |
| 20 | +); |
| 21 | + |
| 22 | +$objectManager = Bootstrap::getObjectManager(); |
| 23 | +/** @var StoreManagerInterface $storeManager */ |
| 24 | +$storeManager = $objectManager->get(StoreManagerInterface::class); |
| 25 | +/** @var ProductAttributeRepositoryInterface $attributeRepository */ |
| 26 | +$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class); |
| 27 | +/** @var ProductRepositoryInterface $productRepository */ |
| 28 | +$productRepository = $objectManager->create(ProductRepositoryInterface::class); |
| 29 | +/** @var CategoryRepositoryInterface $categoryRepository */ |
| 30 | +$categoryRepository = $objectManager->create(CategoryRepositoryInterface::class); |
| 31 | +/** @var GetCategoryByName $getCategoryByName */ |
| 32 | +$getCategoryByName = $objectManager->create(GetCategoryByName::class); |
| 33 | +$category = $getCategoryByName->execute('Category 1'); |
| 34 | +$attribute = $attributeRepository->get('test_configurable'); |
| 35 | + |
| 36 | +$firstProduct = $productRepository->get('simple1'); |
| 37 | +$firstProduct->setData('test_configurable', $attribute->getSource()->getOptionId('Option 1')); |
| 38 | +$firstProduct->setStockData( |
| 39 | + [ |
| 40 | + 'qty' => 0, |
| 41 | + ] |
| 42 | +); |
| 43 | +$productRepository->save($firstProduct); |
| 44 | + |
| 45 | +$secondProduct = $productRepository->get('simple2'); |
| 46 | +$secondProduct->setData('test_configurable', $attribute->getSource()->getOptionId('Option 2')); |
| 47 | +$secondProduct->setStockData( |
| 48 | + [ |
| 49 | + 'qty' => 0, |
| 50 | + ] |
| 51 | +); |
| 52 | +$productRepository->save($secondProduct); |
| 53 | + |
| 54 | +$thirdProduct = $productRepository->get('simple3'); |
| 55 | +$thirdProduct->setData('test_configurable', $attribute->getSource()->getOptionId('Option 2')); |
| 56 | +$thirdProduct->setStockData( |
| 57 | + [ |
| 58 | + 'qty' => 0, |
| 59 | + ] |
| 60 | +); |
| 61 | +$thirdProduct->setStatus(Status::STATUS_ENABLED); |
| 62 | +$productRepository->save($thirdProduct); |
| 63 | + |
| 64 | +$oldStoreId = $storeManager->getStore()->getId(); |
| 65 | +$storeManager->setCurrentStore(Store::DEFAULT_STORE_ID); |
| 66 | +$category->addData(['available_sort_by' => 'position,name,price,test_configurable']); |
| 67 | +try { |
| 68 | + $categoryRepository->save($category); |
| 69 | +} finally { |
| 70 | + $storeManager->setCurrentStore($oldStoreId); |
| 71 | +} |
0 commit comments