woocommerce icon indicating copy to clipboard operation
woocommerce copied to clipboard

Creating a Product Category

Open stevenhardy-digital opened this issue 5 years ago • 6 comments

Hello,

I am trying to create a product with a product category and thumbnail (not worked on this yet).

I am hoping you can help. How would you recommend doing this please?

$product = Product::create([
                'post_title' => 'Product name',
                'post_content' => 'Post description',
                'post_status' => 'publish',
                'post_type' => 'product'
                ]
        );
        $product->createMeta([
            '_sku' => $row['skucode'],
            '_regular_price' => '10.00',
            '_sale_price' => '5.00',
            '_thumbnail_id' => 10
            // other wp_postmeta product meta values...
        ]);

        $category = ProductCategory::create([
            'cat_name' => 'Test'
        ]);

        $product->categories()->save($category);

How can I create a product and attach a category to it, please?

stevenhardy-digital avatar Jan 17 '21 12:01 stevenhardy-digital

Hello! categories relation on Product model is many-to-many relation. You can read about attaching, detaching and syncing related models on that relation in Laravel documentation: https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships.

Your last line of code should look like this:

$product->categories()->sync($category);

or like this, if you want to leave previously added categories untouched:

$product->categories()->attach($category);

Dartui avatar Jan 17 '21 13:01 Dartui

Hello @Dartui Thank you for your reply.

However, I have tried this and I am getting this error:

   Illuminate\Database\Eloquent\MassAssignmentException 

  Add [cat_name] to fillable property to allow mass assignment on [Corcel\WooCommerce\Model\ProductCategory].

Do you have any suggestions please?

stevenhardy-digital avatar Jan 17 '21 16:01 stevenhardy-digital

Column for category name is name, not cat_name. Change you code to something like this:

$category = ProductCategory::create([
    'name' => 'Test'
]);

Also you should set category slug name while creating new category:

$category = ProductCategory::create([
    'name' => 'Test',
    'slug' => 'test',
]);

Dartui avatar Jan 18 '21 09:01 Dartui

@Dartui Thank you for your reply, again I have tried this and I am getting the same error:

  Illuminate\Database\Eloquent\MassAssignmentException 

  Add [name] to fillable property to allow mass assignment on [Corcel\WooCommerce\Model\ProductCategory].
$product = Product::create([
                'post_title' => 'Product name',
                'post_content' => 'Post description',
                'post_status' => 'publish',
                'post_type' => 'product'
            ]
        );
        $product->createMeta([
            '_sku' => $row['skucode'],
            '_regular_price' => '10.00',
            '_sale_price' => '5.00',
            '_thumbnail_id' => 10
            // other wp_postmeta product meta values...
        ]);

        $category = ProductCategory::create([
            'name' => 'Test',
            'slug' => 'test'
        ]);

        $product->categories()->attach($category);

        dd($product);

        return $product;

I definitely think I am missing something somewhere. A really silly question, but do I need to publish the migrations from corcel/corcel package and then it may work? I didnt see this in any readme that I have followed.

Thank you so much for your help! So sorry!

stevenhardy-digital avatar Jan 18 '21 11:01 stevenhardy-digital

[SOLVED] Hello I am facing a similar issue.

I am trying to create a product with an associated image, but I couldn't.

This is the code

$data = request()->validate([ 'nombreArticulo' => 'required', "descripcion" => 'required', "precio" => 'required', "talle" => 'required', "categoria" => 'required', "principalImage" => ['required','mimes:jpg,png,jpeg', 'max:5048'] ]);

    if ($data['talle']== '') {
        $data['talle'] = 10;
    }

    $slug_product = str_replace(" ", "-", $data['nombreArticulo']).$id;


    $image = $this->storeImage($request);

    $product = new producto;
    $product->post_title = $data['nombreArticulo'];
    $product->post_name = $slug_product;
    $product->post_type = 'product';
    $product->save();

    $product->createMeta([
        '_sku' => $id,
        '_regular_price' => $data['precio'],
        '_price' => $data['precio'],
        '_stock_quantity' => 1,
        '_sold_individually' => true,
        '_reviews_allowed' => false,
        '_image' => $image,
    ]);

    $productUploaded = producto::find($product->ID);

    dd($productUploaded);

but I couldn't associate the image

Does anyone have an idea of how to face this?

kitojazz avatar Nov 15 '22 02:11 kitojazz

How can I detach the category from the product?

kitojazz avatar Feb 22 '23 02:02 kitojazz