1

I want to create products programatically by reading an xml file. Reading from xml, and creating product part is completed. I have no idea how to assign the products to category. This is my xml node for the categories.

<categories>Default Category/Wine,Default Category/Collections,Default Category/Spirits,Default Category/Spirits/VODKA/Plain,Default Category for Store 2/Collections,Default Category for Store 2/Wine,Default Category for Store 2/Spirits,Default Category for Store 2/Spirits/VODKA/Plain
    </categories>

Extracted categories from the categories xml node below

Default Category/Wine

Default Category/Collections

Default Category/Spirits,

Default Category/Spirits/VODKA/Plain

Default Category for Store 2/Collections

Default Category for Store 2/Wine

Default Category for Store 2/Spirits

Default Category for Store 2/Spirits/VODKA/Plain

To set the categories i want to use below code set

$category_id= array(4,5);
// add your catagory id
$set_product->setCategoryIds($category_id);

How can i get the category ids from the xml node <categories>...</categories>.

I can get the category id of a last word of category by using below code.

$categoryTitle = 'Wine';
$collection = $this->_categoryFactory->create()->getCollection()
          ->addAttributeToFilter('name',$categoryTitle)->setPageSize(1);
if ($collection->getSize()) {
   $categoryId = $collection->getFirstItem()->getId();
}

But i cannot use above code , because i have 2 stores and categories belongs to two stores.Please help me to complete this task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.