Bagisto Forum

    Bagisto

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    How to create category and product programmatically.

    Knowledge Base
    3
    7
    877
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      alggan4 last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • sanjay-webkul
        sanjay-webkul last edited by

        Hi there,
        Can you please elaborate? what you are trying to do exactly

        1 Reply Last reply Reply Quote 0
        • A
          alggan4 last edited by

          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

          1 Reply Last reply Reply Quote 0
          • A
            alggan4 last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • A
              Amitk-Webkul last edited by

              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

              https://github.com/bagisto/bagisto/blob/v1.4.5/packages/Webkul/Product/src/Http/Controllers/ProductController.php#L156

              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

              1 Reply Last reply Reply Quote 0
              • A
                alggan4 last edited by

                Hi there.
                Thanks for response.
                I checked code. It's the same as v1.4.5. I didn't make any changes, only in my package.

                I found the data is stored in DB with correct (which I pass to the script) location and channel. But they don't show up in the admin panel.

                admin panel.png

                db.png

                ш

                1 Reply Last reply Reply Quote 0
                • A
                  alggan4 last edited by

                  @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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post