44
55use Magento \Catalog \Api \CategoryLinkRepositoryInterface ;
66use Magento \Catalog \Api \CategoryRepositoryInterface ;
7+ use Magento \Catalog \Model \Category ;
8+ use \Magento \Catalog \Model \ResourceModel \Category as CategoryResource ;
79use Magento \Catalog \Api \Data \CategoryInterface ;
810use Magento \Catalog \Api \Data \CategoryProductLinkInterface ;
911use Magento \Catalog \Api \Data \CategoryProductLinkInterfaceFactory ;
@@ -21,6 +23,11 @@ class CategoryBuilder
2123 */
2224 private $ categoryRepository ;
2325
26+ /**
27+ * @var CategoryResource
28+ */
29+ private $ categoryResource ;
30+
2431 /**
2532 * @var CategoryLinkRepositoryInterface
2633 */
@@ -38,12 +45,14 @@ class CategoryBuilder
3845
3946 public function __construct (
4047 CategoryRepositoryInterface $ categoryRepository ,
48+ CategoryResource $ categoryResource ,
4149 CategoryLinkRepositoryInterface $ categoryLinkRepository ,
4250 CategoryProductLinkInterfaceFactory $ productLinkFactory ,
4351 CategoryInterface $ category ,
4452 array $ skus
4553 ) {
4654 $ this ->categoryRepository = $ categoryRepository ;
55+ $ this ->categoryResource = $ categoryResource ;
4756 $ this ->categoryLinkRepository = $ categoryLinkRepository ;
4857 $ this ->category = $ category ;
4958 $ this ->skus = $ skus ;
@@ -60,9 +69,11 @@ public static function topLevelCategory(ObjectManagerInterface $objectManager =
6069
6170 $ category ->setName ('Top Level Category ' );
6271 $ category ->setIsActive (true );
72+ $ category ->setPath ('1/2 ' );
6373
6474 return new self (
6575 $ objectManager ->create (CategoryRepositoryInterface::class),
76+ $ objectManager ->create (CategoryResource::class),
6677 $ objectManager ->create (CategoryLinkRepositoryInterface::class),
6778 $ objectManager ->create (CategoryProductLinkInterfaceFactory::class),
6879 $ category ,
@@ -83,10 +94,11 @@ public static function childCategoryOf(
8394
8495 $ category ->setName ('Child Category ' );
8596 $ category ->setIsActive (true );
86- $ category ->setParentId ($ parent ->getId ());
97+ $ category ->setPath ($ parent ->getCategory ()-> getPath ());
8798
8899 return new self (
89100 $ objectManager ->create (CategoryRepositoryInterface::class),
101+ $ objectManager ->create (CategoryResource::class),
90102 $ objectManager ->create (CategoryLinkRepositoryInterface::class),
91103 $ objectManager ->create (CategoryProductLinkInterfaceFactory::class),
92104 $ category ,
@@ -147,16 +159,20 @@ public function build() : CategoryInterface
147159 $ builder ->category ->setData ('url_key ' , sha1 (uniqid ('' , true )));
148160 }
149161
150- $ category = $ builder ->categoryRepository ->save ($ builder ->category );
162+ // Save with global scope if not specified otherwise
163+ if ($ builder ->category instanceof Category && !$ builder ->category ->hasData ('store_id ' )) {
164+ $ builder ->category ->setStoreId (0 );
165+ }
166+ $ builder ->categoryResource ->save ($ builder ->category );
151167
152168 foreach ($ builder ->skus as $ position => $ sku ) {
153169 /** @var CategoryProductLinkInterface $productLink */
154170 $ productLink = $ builder ->productLinkFactory ->create ();
155171 $ productLink ->setSku ($ sku );
156172 $ productLink ->setPosition ($ position );
157- $ productLink ->setCategoryId ($ category ->getId ());
173+ $ productLink ->setCategoryId ($ builder -> category ->getId ());
158174 $ builder ->categoryLinkRepository ->save ($ productLink );
159175 }
160- return $ category ;
176+ return $ builder -> category ;
161177 }
162178}
0 commit comments