Hello,
I have migrated my database from Magento 1 to Magento 2.3.3 and everything is working fine, except minicart url's which I edited in filre Defaultitem.php here is the code:
if ($this->item->getRedirectUrl()) {
return $this->item->getRedirectUrl();
}
$product = $this->item->getProduct();
$option = $this->item->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}
$categoryIds = $product->getCategoryIds();
if (!$categoryIds) {
return null;
}
$_category = $this->catmodel->load($categoryIds[0]);
//return $_category->getUrl();
return explode('.html', $_category->getUrl())[0] . '/' . $product->getUrlKey() . '.html';
For every *old* product that got migrated from Magento 1 it is working fine I get url like:
http://beta.pneumatig/mycategoryname/subcategory/subcat2/subcat3/wkretarka-cp-10-005-02-nm-1000-obrmin.html <-- product
but Im also dynamically creating new products based on user input and whenever that new product is added to the cart, the following url shows in minicart:
http://beta.pneumatig/catalog/category/view//silownik-profilowy-serii-h-32-325.html <- product
How do I fix that??
Here is my product create function:
$product = $this->productFactory->create();
$product->setSku($this->_getSku());
$product->setName($this->_getName());
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
$product->setVisibility(4);
$product->setPrice(strval($this->_cylinderPrice));
$product->setAttributeSetId(25);
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$product->setTaxClassId(1);
//112 = Pneumatig
$product->setManufacturer(112);
$product->setCountryOfManufacture('PL');
$product->setDescription('Test opis');
$product->setShortDescription('Test short opis');
//Custom atrybuty
$product->setCustomAttribute('pg_cylinder_dia', $this->getDiameter());
$product->setCustomAttribute('pg_cylinder_stroke', $this->getStroke());
$categoryIds = $this->productRepository->get($this->_originalSku);
$categoryIds = $categoryIds->getCategoryIds();
$product->setCategoryIds([intval($categoryIds[0]), intval($categoryIds[1])]);
$url = preg_replace('#[^0-9a-z]+#i', '-', $this->_getName());
$url = preg_replace('#x-#i', '', $url);
$url = strtolower($url);
$product->setUrlKey($url);
if (file_exists($this->_getImage())) $product->addImageToMediaGallery($this->_getImage(), array('image', 'small_image', 'thumbnail'), false, false);
//$product->setShortDescription($this->_getImage());
$product = $this->productRepository->save($product); // This is important - the version provided and the version returned will be different objects
//STOCK
$stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
$stockItem->setIsInStock(1);
$stockItem->setQty(0);
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
return $product->getId();
Please help me out.