Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a Product Category #21

Open
stevenhardy-digital opened this issue Jan 17, 2021 · 6 comments
Open

Creating a Product Category #21

stevenhardy-digital opened this issue Jan 17, 2021 · 6 comments
Labels
question Further information is requested

Comments

@stevenhardy-digital
Copy link

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?

@Dartui Dartui added the question Further information is requested label Jan 17, 2021
@Dartui
Copy link
Collaborator

Dartui commented Jan 17, 2021

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);

@stevenhardy-digital
Copy link
Author

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?

@Dartui
Copy link
Collaborator

Dartui commented Jan 18, 2021

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',
]);

@stevenhardy-digital
Copy link
Author

stevenhardy-digital commented Jan 18, 2021

@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!

@kitojazz
Copy link

kitojazz commented Nov 15, 2022

[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
Copy link

How can I detach the category from the product?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants