cancel
Showing results for 
Search instead for 
Did you mean: 

WebsiteId and categories are not being stored on product

WebsiteId and categories are not being stored on product

Hi

I know that it might be a really simple mistake that I have done, but I am unable to assign the website ID, when programmatically creating products.

The task I am working with is importing product information from a PIM-system into our Magento 2.2 webshop. The data to import consists of categories, products and image paths for both.

Categories are importing fine and show up on the website, but it does not seem to save the website Id.

I create the product using an instance of

\Magento\Catalog\Model\ProductFactory

I then create the Product with this code:

 

 

$categoryLinkManager = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
$item = $productFactory->create(); $item->setSku((string)$fields->ItemNumber->Data); $item->setStoreId($storeId); $item->setWebsiteId([1]); $item->setTypeId('simple'); $item->setAttributeSetId(4); $item->setVisibility(4); $item->setIsActive(true); $item->setName((string)$fields->Name); $item->setUrlKey( trim( preg_replace( '/ +/', '', preg_replace( '/[^A-Za-z0-9 ]/', '-', urldecode( html_entity_decode( strip_tags( strtolower($fields->ItemNumber->Data . '-' . $fields->Name) ) ) ) ) ) ) ); $item->setStockData([ 'use_config_manage_stock' => 0, 'manage_stock' => 0, 'min_sale_qty' => (int)$fields->ItemExtraditionMultiple->Data, 'is_in_stock' => ((string)$fields->ItemLifeCyclus->Data != 'P') ? 1 : 0, 'enable_qty_increments' => ((int)$fields->ItemExtraditionMultiple->Data > 1) ? 1 : 0, 'qty_increments' => ((int)$fields->ItemExtraditionMultiple->Data > 1) ? (int)$fields->ItemExtraditionMultiple->Data : 1 ]); $item->setDescription(nl2br($fields->Description));
// Assign categories
if (array_key_exists('product_' . (string)$fields->ItemNumber->Data, $product_category))
{
    foreach ($product_category['product_' . (string)$fields->ItemNumber->Data] as $category)
{
        $cat = $categoryModel->getCollection()->addAttributeToFilter('category_pim_id', $category)->getFirstItem();
        if ($cat->getId()) {
            if (!$cat->getIsActive()) {
                $assign[] = $cat->getId();
            }
        }
    }
    if (count($assign)) {
        $item->setCategoryIds($assign);
    }
}
$item->save();
$categoryLinkManager->assignProductToCategories(
    $item->getSku(),
    $assign
);

The above code is an extraction of the part of the code used to create products. The entire code also includes all the custom attributes defined to store the PIM-data as well as logic to handle skipping products that do already exist.

 

First problem I have is that the WebsiteId does not seem to assign to the products, which means that I cannot see it assigned to "Main Website" under Catalog -> Products.

Additionally

I have tried to assign it like an array in the above code, but also just as a number like this:

$item->setWebsiteId(1);

Also, the categories do not seem to assign correctly to the products. When I go to Catalog -> Categories, I can see the products assigned to each category, but when I go to Catalog -> Products, I cannot see which categories are assigned to the product?

 

Could someone shine a light on what I am missing or what I am doing wrong?

 

2 REPLIES 2

Re: WebsiteId and categories are not being stored on product

Does anybody have an idea for why the website_id and categories are not persisting correctly when related to products?

Re: WebsiteId and categories are not being stored on product

We are willing to pay for getting the issue resolved.

Send me a PM if you will be able to assist with resolving the issue