Creating a Product Category
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?
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);
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?
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 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!
[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?
How can I detach the category from the product?