1212use Magento \Catalog \Model \Product \Attribute \Source \Status ;
1313use Magento \Catalog \Model \Product \Type ;
1414use Magento \Catalog \Model \Product \Visibility ;
15+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
1516use Magento \CatalogSearch \Model \Indexer \Fulltext ;
1617use Magento \CatalogUrlRewrite \Observer \ProductProcessUrlRewriteSavingObserver ;
1718use Magento \Framework \App \ObjectManager ;
1819use Magento \Framework \App \ResourceConnection ;
19- use Magento \Framework \App \State ;
2020use Magento \Framework \Event \Config \Data ;
2121use Magento \Framework \Event \ConfigInterface ;
2222use Magento \Framework \Exception \NoSuchEntityException ;
23- use Magento \Framework \Exception \StateException ;
24- use Magento \Framework \Indexer \IndexerRegistry ;
2523use Magento \Framework \Registry ;
2624use Magento \Store \Api \WebsiteRepositoryInterface ;
27- use Magento \ TestFramework \ Helper \ Bootstrap ;
25+ use Yireo \ GoogleTagManager2 \ Test \ Integration \ Stub \ FulltextStub ;
2826
2927trait CreateProduct
3028{
@@ -33,6 +31,18 @@ public function createProduct(
3331 array $ data = []
3432 ): ProductInterface {
3533 $ objectManager = ObjectManager::getInstance ();
34+ /*$objectManager->configure([
35+ 'preferences' => [
36+ Fulltext::class => FulltextStub::class
37+ ]
38+ ]);*/
39+
40+ /*
41+ $indexerFactory = $objectManager->get(\Magento\Indexer\Model\IndexerFactory::class);
42+ /** @var \Magento\Indexer\Model\Indexer $indexer */
43+ //$indexer = $indexerFactory->create()->load('catalogsearch_fulltext');
44+ //$indexer->reindexAll();
45+
3646 $ productFactory = $ objectManager ->get (ProductInterfaceFactory::class);
3747 $ defaultCategory = $ objectManager ->get (DefaultCategory::class);
3848 $ productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
@@ -58,14 +68,21 @@ public function createProduct(
5868 ->setWebsiteIds ([$ this ->getDefaultWebsiteId ()])
5969 ->setAttributeSetId ($ this ->getDefaultAttributeSetId ())
6070 ->setVisibility (Visibility::VISIBILITY_BOTH )
61- ->setStockData (['use_config_manage_stock ' => 0 ])
6271 //->setCanSaveCustomOptions(true)
6372 //->setHasOptions(true)
6473 ->addData ($ data );
6574
6675 $ product ->isObjectNew (true );
6776 $ productRepository ->save ($ product );
6877
78+ $ stockRegistry = $ objectManager ->get (StockRegistryInterface::class);
79+ $ stockItem = $ stockRegistry ->getStockItemBySku ($ product ->getSku ());
80+ $ stockItem ->setUseConfigManageStock (1 );
81+ $ stockItem ->setIsInStock (1 );
82+ $ stockItem ->setProductId ($ product ->getId ());
83+ $ stockItem ->setQty (9999 );
84+ $ stockRegistry ->updateStockItemBySku ($ product ->getSku (), $ stockItem );
85+
6986 if (!empty ($ product ->getCategoryIds ())) {
7087 $ categoryLinkManagement = $ objectManager ->get (CategoryLinkManagementInterface::class);
7188 $ categoryLinkManagement ->assignProductToCategories (
@@ -74,6 +91,8 @@ public function createProduct(
7491 );
7592 }
7693
94+
95+
7796 return $ product ;
7897 }
7998
0 commit comments