How to create category and product programmatically.
-
I'm beginner in bagisto and I need help.
I try to write code wich will be import data from previus ecommerce.
Please share example of code, how to create programmatically category and product (with images) and best way to find category by slug.
Thanks a lot. -
Hi there,
Can you please elaborate? what you are trying to do exactly -
I need smthing like this: https://forums.bagisto.com/topic/3256/create-category-using-categoryrepository
But working variant for creating Category and variant for creating simple product - 22 days later
-
Hi there,
So with category creation everything is ok.
But problem with ptoduct.
I creating products using this code:// start snipet foreach ($info as $row) { $product = $this->productRepository->create([ 'type' => 'simple', 'attribute_family_id' => 1, 'sku' => $this->createSku($row) ]); $channel = $this->getCurrentChannel(); $data['channel'] = $channel->id; $data['channels'] = [ 0 => $channel->id, ]; $data['locale'] = 'en';//$this->getCurrentLocale()->code; $data['url_key'] = $row['url']; $data['status'] = true; $data['product_number'] = $row['vendor_code']; $data['weight'] = $row['weight']; $data['price'] = $row['price_in']; if ($brand = Attribute::where(['code' => 'brand'])->first()) { $data['brand'] = AttributeOption::where(['attribute_id' => $brand->id])->firstOrCreate(['admin_name' => $row['brand'], 'attribute_id' => $brand->id])->id ?? ''; } $inventorySource = $channel->inventory_sources[0]; $data['inventories'] = [ $inventorySource->id => DEF_QTY, ]; $data['categories'] = []; foreach ($row['tids'] as $tid) { $legacyCategoryLink = LegacyCategoryLink::where('tid', $tid)->first(); if ($legacyCategoryLink && $legacyCategoryLink->category_id) { $data['categories'][] = $legacyCategoryLink->category_id; } } /** * this part not stored in DB */ $data['cost'] = $row['price']; $data['meta_title'] = $row['meta_title']; $data['meta_description'] = $row['meta_description']; $data['name'] = $row['title']; $data['short_description'] = $row['desc1']; $data['description'] = $row['desc2']; /** * end */ $updated = $this->productRepository->update($data, $product->id); Event::dispatch('catalog.product.update.after', $product); } /// end snippet
So almost works correctly, with the exception of part market as "not stored in DB"
Looks like problem with attributes which can have different values depending of locales or channels.Can anyone suggest a correct example of saving these values: name, short_description, description, meta_description, meta_title, cost.
Thanks.
-
Hi there.
Please check that your code is implemented in these repositories. If still facing the same issue please let me know.Product Controller Link: https://github.com/bagisto/bagisto/blob/v1.4.5/packages/Webkul/Product/src/Http/Controllers/ProductController.php#L95
Product Repository Link: https://github.com/bagisto/bagisto/blob/v1.4.5/packages/Webkul/Product/src/Repositories/ProductRepository.php#L70
Listener Link: https://github.com/bagisto/bagisto/blob/v1.4.5/packages/Webkul/Product/src/Listeners/ProductFlat.php#L139
Abstract Link: https://github.com/bagisto/bagisto/blob/v1.4.5/packages/Webkul/Product/src/Type/AbstractType.php#L167
-
-
@alggan4 said in How to create category and product programmatically.:
$data['channel'] = $channel->id;
here must be
$data['channel'] = $channel->code;p.s.
and don't forget that url_key shouldn't contain "/"thanks to all