Skip to main content

Product

Creating a product

Use the create method to create a new product. A Product requires a name and a price:

1use Larasell\Larasell\Models\Product;
2
3$product = Product::create([
4    'name' => 'Bicycle',
5    'price' => 1999,
6]);

A product without a price is not useful, so Larasell won't let you create one.

Prices

Products need prices, adding one is just one line:

1use Larasell\Larasell\Models\Product;
2
3Product::create([
4  'name' => 'Beer',
5  'price' => 200,
6]);

In addition, you can also pass a string or a Price class:

 1use Larasell\Larasell\Models\Product;
 2use Larasell\Larasell\Price;
 3
 4Product::create([
 5  'name' => 'Pretzel',
 6  'price' => '100',
 7]);
 8
 9Product::create([
10  'name' => 'Sausage',
11  'price' => Price::of(300),
12]);

Formatting prices

To format prices you need to pass a currency, either as a Currency enum or a simple string.

1use Larasell\Larasell\Enums\Currency;
2use Larasell\Larasell\Price;
3
4Price::format($product->price, 'EUR'); // '€12.99'
5Price::format($product->price, Currency::USD); // '$12.99'

Prices look different depending on the locale. For example, in German the currency symbol comes after the amount.

You can use the third parameter to format prices for different locales.

1use Larasell\Larasell\Enums\Currency;
2
3Price::format($product->price, Currency::EUR, 'de-DE'); // '12,99 €'
4Price::format($product->price, Currency::EUR, 'en-US'); // '€12.99'

Compare-at prices

Compare-at prices make products look like they're on sale or can make it look like customers are getting a better deal.

Set the price to the reduced rate — this is the price the customer will pay. The compare_at price is displayed as the original price, making the product look like it's on sale.

1use Larasell\Larasell\Models\Product;
2
3$product = Product::create([
4    'name' => 'T-Shirt',
5    'price' => Price::of(1299),
6    'compare_at' => Price::of(1999)
7]);

The onSale method makes it easy to show that a product is on sale.

1use Larasell\Larasell\Models\Product;
2
3$product->onSale();

For the full price documentation, take a look at the reference page.

Visibility

It's possible to define the visibility of a Product. Common use cases are preparing for a new launch, or syncing products from a different system and making them available for purchase later.

1use Larasell\Larasell\Enums\Visibility;
2use Larasell\Larasell\Models\Product;
3
4$product = Product::create([
5    'name' => 'Bicycle',
6    'price' => 20_000,
7    'status' => Visibility::Hidden,
8]);

Then at a later point you can make it visible, and it will show up on the storefront.

1use Larasell\Larasell\Enums\Visibility;
2use Larasell\Larasell\Models\Product;
3
4$product = Product::where('name->en', 'Bicycle')->first();
5
6$product->update([
7  'status' => Visibility::Visible // or pass 'visible'
8]);

A product can either be hidden or visible. If you want, you can just pass a string instead of passing the Visibility enum.

Products have a visible query scope, meaning you can filter out hidden products with a single line of code.

1use Larasell\Larasell\Models\Product;
2
3$products = Product::visible()->get();

Query products via categories

Often times you need to query products based on the category they belong to. Product has two methods to help you with that. inCategory gets you all the products that are part of a category:

1use Larasell\Larasell\Models\Category;
2use Larasell\Larasell\Models\Product;
3
4$category = Category::where('slug->en', 'clothing')->firstOrFail();
5
6$products = Product::inCategory($category)->get();

This returns all the products that are part of the clothing category. But it won't return products from sub-categories like Clothing -> Shoes. If you need to do that, you can use the inCategoryTree method:

1use Larasell\Larasell\Models\Category;
2use Larasell\Larasell\Models\Product;
3
4$category = Category::where('slug->en', 'clothing')->firstOrFail();
5
6$products = Product::inCategoryTree($category)->get();

Now you'll get all products that are inside Clothing and its sub-categories.

To learn how to add and remove products from a category, take a look at the category reference.

Stock

By default, the product stock is null, which means customers can purchase this product indefinitely. This is useful for digital products, but not for physical products.

To stock a product, set an integer value for stock on the Eloquent model:

 1use Larasell\Larasell\Models\Product;
 2
 3$product = Product::create([
 4    'name' => 'Poster',
 5    'price' => 1999,
 6    'stock' => 500,
 7]);
 8
 9$variant = $product->defaultVariant();
10$variant->availableStock(); // 500

By default, backorders are enabled for products. Backorders allow you to continue selling a product even after it is out of stock.

To prevent this, set the value for allow_backorders to false:

 1use Larasell\Larasell\Models\Product;
 2
 3$product = Product::create([
 4    'name' => 'Poster',
 5    'price' => 1999,
 6    'stock' => 500,
 7    'allow_backorders' => false,
 8]);
 9
10$variant = $product->defaultVariant();
11$variant->allowsBackorders(); // false

Sometimes you want to make sure customers can only add a handful of products to a cart:

 1use Larasell\Larasell\Models\Product;
 2
 3$socks = Product::create([
 4    'name' => 'Socks',
 5    'price' => 300,
 6    'stock' => 500,
 7    'min_quantity' => 6,
 8]);
 9
10$variant = $socks->defaultVariant();
11$variant->minimumQuantity(); // 6

And other times you just want to make sure they cannot buy everything:

 1use Larasell\Larasell\Models\Product;
 2
 3$handSanitizer = Product::create([
 4    'name' => 'Hand Sanitizer',
 5    'price' => 250,
 6    'stock' => 30,
 7    'max_quantity' => 4,
 8]);
 9
10$variant = $handSanitizer->defaultVariant();
11$variant->maximumQuantity(); // 4

The min_quantity can never be larger than the max_quantity. If that's the case Larasell will throw an exception:

 1use Larasell\Larasell\Models\Product;
 2
 3$product = Product::create([
 4    'name' => 'Beer Crate',
 5    'price' => 250,
 6    'stock' => 30,
 7    'min_quantity' => 10,
 8]);
 9
10$product->max_quantity = 7; // Throws InvalidArgumentException

You don't have to enforce these limits yourself. When you add products to a Cart, Larasell checks the stock, backorder and quantity rules automatically and throws a descriptive exception if a rule is violated.

Translating the product

It's possible to translate certain fields of a product, like the name, slug and description. Translations are keyed by locale, written as a BCP 47 tag like en, de or de-DE.

1use Larasell\Larasell\Models\Product;
2
3Product::create([
4  'name' => ['en' => 'Bicycle', 'de' => 'Fahrrad'],
5  'price' => 200,
6]);

To get the translated value, you call the get method on the product. It uses App::currentLocale() to determine which translation to return to you.

1$product->name->get();

In case you want to get the translation for a specific locale, you can pass an optional string parameter to the method.

1$product->name->get('de');

If the product has no translation for the locale, Larasell falls back to a shorter tag first, so de-AT returns the de translation. After that it uses the shop's default locale from config/larasell/locale.php, and finally the first translation the product has. The locale reference explains the full order.

Translating an existing product is easy too.

1use Larasell\Larasell\Models\Product;
2
3$bicycle = Product::where('slug->en', 'bicycle')->first();
4
5$bicycle->update([
6  'name' => ['nl' => 'Fiets']
7]);

Note that calling update as above overwrites all previous translations. If you only want to add a translation, you have to call the with method on the translatable value.

1$bicycle->update(['name' => $bicycle->name->with('fr', 'Vélo')]);

In this case, we added a translation for French. If it already existed, we would overwrite the value rather than add it.

The same is true for removing a translation. Call the without method to remove a single translation without removing all translations.

1$bicycle->update(['name' => $bicycle->name->without('de')]);

The all method helps you see all the translations the product has.

1$bicycle->name->all(); // ['en' => 'Bicycle', 'nl' => 'Fiets', 'de' => 'Fahrrad']

There's a has method that you can call to check whether a product has a translation for a specific locale.

1$bicycle->name->has('pt');

Locale keys are stored in their canonical form. Passing de_DE or de-de stores the translation under de-DE, so with, without, has and get all accept either style.

The translatable fields on a product are name, slug and description. To serve a shop in several languages, see the localization guide.

Weights

You can define a weight and dimensions for a product. This is useful to evaluate available shipping methods and their prices.

Use Weight to give a product a weight:

 1use Larasell\Larasell\Enums\WeightUnit;
 2use Larasell\Larasell\Models\Product;
 3use Larasell\Larasell\Weight;
 4
 5$product = Product::create([
 6  'name' => 'Bicycle',
 7  'price' => 20_000,
 8  'weight' => Weight::of('12.5', 'kg')
 9]);
10
11$product->weight->amount(); // returns '12.5'
12
13$product->weight->unit(); // returns WeightUnit::Kilogram
14$product->weight->unit()->value; // returns 'kg'

It's possible to convert the weight for calculations or just for presentation:

1use Larasell\Larasell\Enums\WeightUnit;
2
3$product->weight->to(WeightUnit::Pound)->amount(); // '27.55778277'
4$product->weight->amount(WeightUnit::Ounce); // '440.9245243'

The above code does not update the weight on the product. The product still uses the kilograms. To update the product you need to call the to method:

1use Larasell\Larasell\Enums\WeightUnit;
2
3$product->update([
4  'weight' => $product->weight->to(WeightUnit::Pound)
5]);

Dimensions

To add dimensions to products use Dimensions:

1use Larasell\Larasell\Dimensions;
2use Larasell\Larasell\Models\Product;
3
4$product = Product::create([
5  'name' => 'Poster',
6  'price' => 1999,
7  'dimensions' => Dimensions::of(50, 35, 5, 'cm')
8]);

Once you have a dimension you can get the length, width and height of a product:

1$product->dimensions->length(); // returns Length::of(50, 'cm')
2$product->dimensions->length()->amount(); // '50'
3$product->dimensions->length()->unit(); // returns LengthUnit::Centimeter
4
5$product->dimensions->width(); // returns Length::of(35, 'cm')
6$product->dimensions->height(); // returns Length::of(5, 'cm')

There's also a way to get the volume of a product:

1$product->dimensions->volume(); // returns '87500'; 50 × 35 × 5 cm³

To convert the dimensions to a different unit make use of the to method. Keep in mind that to creates a new Dimensions class and does not update the existing value, because the class is immutable.

1use Larasell\Larasell\Enums\LengthUnit;
2
3$product->update([
4  'dimensions' => $product->dimensions->to(LengthUnit::Millimeter)
5]);
6
7$product->dimensions->length()->amount(); // '500'

SKUs and barcodes

Larasell allows you to add SKUs to a product:

1use Larasell\Larasell\Models\Product;
2
3$product = Product::create([
4  'name' => 'Bicycle',
5  'price' => 1_000,
6  'sku' => 'BIC-001'
7]);
8
9$sku = $product->sku;

A product can also have a barcode:

1use Larasell\Larasell\Models\Product;
2
3$product = Product::create([
4  'name' => 'Bicycle',
5  'price' => 1_000,
6  'barcode' => '4006381333931'
7]);
8
9$barcode = $product->barcode;

The barcode is stored as a string. If you want to check that it is a real GTIN (EAN-8, UPC-A, EAN-13, or GTIN-14), wrap it in Barcode first. The class strips spaces and hyphens. A missing or wrong check digit throws an InvalidArgumentException.

1use Larasell\Larasell\Barcode;
2
3Barcode::of('4006381333931');
4Barcode::of('400 6381-333931'); // same digits