|
| 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\Eav\Api\AttributeRepositoryInterface; |
| 9 | +use Magento\Store\Model\Store; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | + |
| 12 | +$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class); |
| 13 | +$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
| 14 | + |
| 15 | +$eavConfig->clear(); |
| 16 | + |
| 17 | +/** @var $installer \Magento\Catalog\Setup\CategorySetup */ |
| 18 | +$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class); |
| 19 | + |
| 20 | +if (!$attribute->getId()) { |
| 21 | + |
| 22 | + /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ |
| 23 | + $attribute = Bootstrap::getObjectManager()->create( |
| 24 | + \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class |
| 25 | + ); |
| 26 | + |
| 27 | + /** @var AttributeRepositoryInterface $attributeRepository */ |
| 28 | + $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class); |
| 29 | + |
| 30 | + /** @var $store \Magento\Store\Model\Store */ |
| 31 | + $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class); |
| 32 | + $store = $store->load('test', 'code'); |
| 33 | + |
| 34 | + $attribute->setData( |
| 35 | + [ |
| 36 | + 'attribute_code' => 'test_configurable', |
| 37 | + 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
| 38 | + 'is_global' => 1, |
| 39 | + 'is_user_defined' => 1, |
| 40 | + 'frontend_input' => 'select', |
| 41 | + 'is_unique' => 0, |
| 42 | + 'is_required' => 0, |
| 43 | + 'is_searchable' => 1, |
| 44 | + 'is_visible_in_advanced_search' => 1, |
| 45 | + 'is_comparable' => 1, |
| 46 | + 'is_filterable' => 1, |
| 47 | + 'is_filterable_in_search' => 1, |
| 48 | + 'is_used_for_promo_rules' => 0, |
| 49 | + 'is_html_allowed_on_front' => 1, |
| 50 | + 'is_visible_on_front' => 1, |
| 51 | + 'used_in_product_listing' => 1, |
| 52 | + 'used_for_sort_by' => 1, |
| 53 | + 'frontend_label' => ['Test Configurable'], |
| 54 | + 'backend_type' => 'int', |
| 55 | + 'option' => [ |
| 56 | + 'value' => ['option_0' => [ |
| 57 | + Store::DEFAULT_STORE_ID => 'Option Admin Store', |
| 58 | + Store::DISTRO_STORE_ID => 'Option Default Store', |
| 59 | + $store->getId() => 'Option Test Store' |
| 60 | + ], 'option_1' => ['Option 2']], |
| 61 | + 'order' => ['option_0' => 1, 'option_1' => 2], |
| 62 | + ], |
| 63 | + 'default' => ['option_0'] |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | + $attributeRepository->save($attribute); |
| 68 | + |
| 69 | + /* Assign attribute to attribute set */ |
| 70 | + $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId()); |
| 71 | +} |
| 72 | + |
| 73 | +$eavConfig->clear(); |
| 74 | + |
| 75 | +/** @var \Magento\Framework\ObjectManagerInterface $objectManager */ |
| 76 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 77 | + |
| 78 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 79 | +$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class); |
| 80 | +$product->isObjectNew(true); |
| 81 | +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) |
| 82 | + ->setId(10) |
| 83 | + ->setAttributeSetId(4) |
| 84 | + ->setName('Simple Product1') |
| 85 | + ->setSku('simple1') |
| 86 | + ->setTaxClassId('none') |
| 87 | + ->setDescription('description') |
| 88 | + ->setShortDescription('short description') |
| 89 | + ->setOptionsContainer('container1') |
| 90 | + ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART) |
| 91 | + ->setPrice(10) |
| 92 | + ->setWeight(1) |
| 93 | + ->setMetaTitle('meta title') |
| 94 | + ->setMetaKeyword('meta keyword') |
| 95 | + ->setMetaDescription('meta description') |
| 96 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 97 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 98 | + ->setWebsiteIds([1]) |
| 99 | + ->setCategoryIds([]) |
| 100 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
| 101 | + ->setSpecialPrice('5.99') |
| 102 | + ->save(); |
| 103 | + |
| 104 | +$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class); |
| 105 | +$product->isObjectNew(true); |
| 106 | +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) |
| 107 | + ->setId(11) |
| 108 | + ->setAttributeSetId(4) |
| 109 | + ->setName('Simple Product2') |
| 110 | + ->setSku('simple2') |
| 111 | + ->setTaxClassId('none') |
| 112 | + ->setDescription('description') |
| 113 | + ->setShortDescription('short description') |
| 114 | + ->setOptionsContainer('container1') |
| 115 | + ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE) |
| 116 | + ->setPrice(20) |
| 117 | + ->setWeight(1) |
| 118 | + ->setMetaTitle('meta title') |
| 119 | + ->setMetaKeyword('meta keyword') |
| 120 | + ->setMetaDescription('meta description') |
| 121 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 122 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 123 | + ->setWebsiteIds([1]) |
| 124 | + ->setCategoryIds([]) |
| 125 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
| 126 | + ->setSpecialPrice('15.99') |
| 127 | + ->save(); |
| 128 | + |
| 129 | +$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class); |
| 130 | +$category->isObjectNew(true); |
| 131 | +$category->setId( |
| 132 | + 333 |
| 133 | +)->setCreatedAt( |
| 134 | + '2014-06-23 09:50:07' |
| 135 | +)->setName( |
| 136 | + 'Category 1' |
| 137 | +)->setParentId( |
| 138 | + 2 |
| 139 | +)->setPath( |
| 140 | + '1/2/333' |
| 141 | +)->setLevel( |
| 142 | + 2 |
| 143 | +)->setAvailableSortBy( |
| 144 | + ['position', 'name'] |
| 145 | +)->setDefaultSortBy( |
| 146 | + 'name' |
| 147 | +)->setIsActive( |
| 148 | + true |
| 149 | +)->setPosition( |
| 150 | + 1 |
| 151 | +)->setPostedProducts( |
| 152 | + [10 => 10, 11 => 11] |
| 153 | +)->save(); |
| 154 | + |
| 155 | +/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */ |
| 156 | +$indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class); |
| 157 | +$indexerCollection->load(); |
| 158 | + |
| 159 | +/** @var \Magento\Indexer\Model\Indexer $indexer */ |
| 160 | +foreach ($indexerCollection->getItems() as $indexer) { |
| 161 | + $indexer->reindexAll(); |
| 162 | +} |
0 commit comments