-
Notifications
You must be signed in to change notification settings - Fork 56
6.0 Getting started with products
Products process is a bit overwhelming as you need to create products, attach them to many things such as category, specific type etc.., approving products, listing them by custom attributes you are searching for through the attached tables to products, no worries ! we got your back and run that behind the scenes.
Alright ! Let's get started .
Generating a product has a couple of concerns such as attaching it to a category, setup the variation for it, but that's already setup to you.
The very first thing to start with as you probably guessed is generating a product.
// within controller.
public function store()
{
/* request()->all() is similar to this
[
'user_id' => auth()->id(),
'name' => 'Fancy T-Shirt',
'description' => 'some random feature about this t-shirt',
'price' => 80,
'category' => 'clothes',
'type' => ['name' => 'lacoste', 'stock' => 20],
'details' => ['color' => 'Red', 'size' => 'XXL'],
]
*/
$this->productRepo->generate(request()->all());
}
I guess you might need to simulate the incoming request to look like this, anyways you know the structure that should be passed to the method , so stick to it.
- The method will retrieve the category,variation and type, then attach it to this product, if they don't exist it will create them firstly then attach them.
- it returns the product that's just created.
You probably guessed since seeing the tables structure, that's just a basic method that's in charge of updating a couple of columns.
$product = Product::first();
$this->productRepo->approve($product->id); // $this->productRepo is equal to Product::approve, as Product facade is actually the prouctRepository.
The method actually checks against the configuration, whether you have the review process enabled or not, and If it's enabled, it just updates the fields that's related to the review process.
as the name implies, all you just need to do is passing a string contains of the category name you are searching for , or even an array of category names.
$products = factory(Product::class,3)->create();
$category = factory(Category::class)->create();
$category->products()->attach($products);
$this->productRepo->fetchByCategories($category->type); // returns a query builder , you can get or chain conditions.
$this->productRepo->fetchByCategories($categories->pluck('type')); // or search by an array of names.
I guess it makes sense that each user creating a product, would be in charge of selling these products at this country or so, therefore there is a method concerning the products within a specific location.
- This method retrieves the products that's created by someone that belongs to the passed locations.
$user = User::first(); // user lives in U.K
// user created many products.
$this->productRepo->fetchByLocation('U.K'); // returns a query builder, you can get them or chain conditions.
$this->productRepo->fetchByLocation(['U.K' , 'U.S' ]);
As the name implies, it fetches the trendy ones using the passed location(s).
$this->productRepo->fetchTrendyByLocation('U.K'); // returns a query builder.
It sorts the products depending on which added to cart most.
You can pass variations you are searching for , or even creating your own custom methods , and chain the methods that the package uses alongside your own methods.
$this->productRepo->fetchByVariations(['locations' => 'Egypt', 'variations' => ['size' => 'XL', 'color' => 'red'], 'categories' => $type]); // returns a collection.
bear in mind that locations, variations and categories are a reserved words that indicates methods that should be called, if you have your own key that you wish to push to this method, you can call the addCriterion method , and since the repository is being called through singleton, you will have this method a long the whole project.
Let's do our own basic example to see it in action.
// within service or module.
$this->productRepo->addCriterion('fetchReviewed' , function($reviewed){
return $this->productInstance->whereReviewed($reviewed);
});
a basic method that should retrieve me the reviewed products only.
$this->productInstance->fetchByVariations(['locations' => 'Egypt','fetchReviewed' => ['reviewed' => true]])->get(); // collection is returned.