Skip to content

6.2 Product Variation Types.

Security Theater edited this page Oct 12, 2018 · 2 revisions

If you meditated the migrations file , you probably noticed that there are stock, price which is nullable ( sort of weird ) and finally the name.

That's a bit confusing, and I assumed you have the following questions.

  • Why stock is here instead of products table ?

Imagine having the stock within the products table, that's so generic as the product itself may have many variations, and each variation has its own stock, so it would be messy having it there instead of here.

  • Why price column exist here and within products table ?

as mentioned in the previous question, each product should have its own price, but what if a specific variation is unique enough to have a custom price, you wouldn't achieve that if there is no price column within the product_variation_types table

Of course within the package's logic, we check on the price if it exists within this table here, and if it doesn't we fallback to the default price that you assigned in the products table, which provides you much flexibility.

  • What's the purpose of having this table and categories table ?

The purpose of having this table is to manage the stock , price of each product on its own, and you can consider the product_variation_types as a sub-category of the main category

For example , you may have a MacBook Pro laptop as product name, Laptop as a product_variation_type and Electronics as Category of this product, and that's really open for the complexity , as the categories can be children of a parent category using the self-referential relationship.

Concerning the stock , you probably thought of a stock managerial within this repository , and you are right, out of the box this repository provides a couple of methods to handle that, but surprisingly these methods are open for other repositories on-demand because simply the stock methods are within a trait, which you can use whenever you want.

 $product = Product::first();
 $this->productTypeVariationRepo->stock($product->types->first()->id); // returns integer.
 $this->productTypeVariationRepo->hasStock($product->types->first()->id); // returns boolean.
 $this->productTypeVariationRepo->inStock($product->types->first()->id); // returns true if the stock is more than 5
 $this->productTypeVariationRepo->lowStock($product->types->first()->id); // returns true if stock is between 0 and 5
 $this->productTypeVariationRepo->incrementStock($product->types->first()->id); // increments the stock by one , returns the incremented type.
 $this->productTypeVariationRepo->incrementStock($product->types->first()->id , 10); // increments the stock by ten , returns the incremented type.
 $this->productTypeVariationRepo->decrementStock($product->types->first()->id); // decrements the stock by one , returns the type instance.
 $this->productTypeVariationRepo->decrementStock($product->types->first()->id , 10); // decrements the stock by one , returns the type instance.
 $this->productTypeVariationRepo->decrementStock($product->types->first()->id , 1000); // exception is thrown if the product doesn't have 1000 in stock.
 

The decrement method , checks if the number passed in the second argument is actually less than or equal to the actual stock then decrement it, if It's larger than the actual stock, an exception is thrown.