2

I want to progrmmatically create products from a xml.

Ex : I am getting the product categories as like below.

Default Category/Beer ,
Default Category/Beer /CRAFT,
Default Category/Beer /CRAFT/BOMBER BEER,
Default Category/BEER,
Default Category for Store 2/Beer ,
Default Category for Store 2/Beer /CRAFT,
Default Category for Store 2/Beer /CRAFT/BOMBER BEER,
Default Category for Store 2/BEERe

I am getting the category path from the xml.I want to assign the category to the product.How can i assign the categories to the product?

Below is the model of xml enter image description here

0

As per given text you cannot find category id.

You can achieve this function using below code.

You need to create category id and category name array. by mapping category name you will get category id, after set category id to products.

protected $_categoryCollectionFactory;

public function __construct(        
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
)
{
    $this->_categoryCollectionFactory = $categoryCollectionFactory;
}

public function getCategoryIds() {
    $collection = $this->_categoryCollectionFactory->create();
    $allCategory = array();
    foreach($collection as $category) {
        $allCategory[$category->getName()] = $category->getId() ;
    }

    return $allCategory;
}
  • thanks, I want to get the category from category path and assign the category to the products – Mujahidh Mar 21 at 14:05
  • Yes you can get last string from path so you will get category name, using this rray you will find category id – Prashant Valanda Mar 21 at 16:34
  • where can i pass the path as an argument here? – Mujahidh Mar 22 at 4:18
  • You do not need to pass any argument, first find category name and call $array = $thi->getCategoryIds() funcll give you all category name and category id array. $array['category name here'] using this code you will get category id – Prashant Valanda Mar 22 at 11:43
  • I am not clear about this,i am getting the product details as xml from a 3rd party tool as xml,from that xml i am getting the categories like this Default Category/Beer /CRAFT,, from this string i want to capture the category ids to assign the products which created progrmatically – Mujahidh Mar 25 at 14:07
0

I manage to do this like below scenario.

  1. Load all the categories.
  2. Get the category path(return path id).
  3. Generate the category path(By name).
  4. Check the created path with My argument loop(Category set) and get the category id and assign to an array.

Below is my helper class and function.

<?php
namespace Eight65media\MpowerSync\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper{

protected $_categoryCollectionFactory;
enter code here
public function __construct(
   \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
){
   $this->_categoryCollectionFactory = $categoryCollectionFactory;
}

public function getCategoryIds($isActive = true, $categories) {

    $collection = $this->_categoryCollectionFactory->create();
    $collection->addAttributeToSelect('*');

// select only active categories
    if ($isActive) {
        $collection->addIsActiveFilter();
    }
foreach ($collection as $category) {
        $id = $category->getId(); //Ex - 20
        $path = $category->getPath(); // Ex - 1/3/15/20
        $path_togo = explode("/",$path);

        $path_as_name =array();

            foreach($path_togo as $paths){
                $cat_id = $paths;
                $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $category_data = $_objectManager->create('Magento\Catalog\Model\Category')
                        ->load($cat_id);
                $cat_name = $category_data->getName();
                $path_as_name[]= $cat_name;
            }
        unset($path_as_name[0]);//Remove the root category from the path  string

        $final=implode('/', $path_as_name);

        $str = $categories;//Category sets passed via the function to get the category id
        $category_data = explode("," , $str);

        foreach($category_data as $cat){
            if( strcmp( $final, $cat ) == 0 ) {
                $final_category[]= $id;
            }
        }
    }
    return $final_category;
}//End of function getCategoryIds

Hope this one help some one in future.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.