Laravel Ecommerce Example
Code for YouTube video series: https://www.youtube.com/watch?v=o5PWIuDTgxg&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR
Website Demo: https://laravelecommerceexample.ca. The demo has limited permissions. Install locally for full access.
Installation
- Clone the repo and
cd
into it composer install
- Rename or copy
.env.example
file to.env
php artisan key:generate
- Set your database credentials in your
.env
file - Set your Stripe credentials in your
.env
file. SpecificallySTRIPE_KEY
andSTRIPE_SECRET
- Set your Algolia credentials in your
.env
file. SpecificallyALGOLIA_APP_ID
andALGOLIA_SECRET
. See this episode. - Set your Braintree credentials in your
.env
file if you want to use PayPal. SpecificallyBT_MERCHANT_ID
,BT_PUBLIC_KEY
,BT_PRIVATE_KEY
. See this episode. If you don't, it should still work but won't show the paypal payment at checkout. - Set your
APP_URL
in your.env
file. This is needed for Voyager to correctly resolve asset URLs. - Set
ADMIN_PASSWORD
in your.env
file if you want to specify an admin password. If not, the default password is 'password' php artisan ecommerce:install
. This will migrate the database and run any seeders necessary. See this episode.npm install
npm run dev
php artisan serve
or use Laravel Valet or Laravel Homestead- Visit
localhost:8000
in your browser - Visit
/admin
if you want to access the Voyager admin backend. Admin User/Password:admin@admin.com/password
. Admin Web User/Password:adminweb@adminweb.com/password
Shopping Cart Package
I originally used the Crinsane/LaravelShoppingcart package but it is slow to update to the latest versions of Laravel. I now use hardevine/LaravelShoppingcart which is a forked version that updates quicker.
Windows Users - money_format() issue
The money_format
function does not work in Windows. Take a look at this thread. As an alternative, just use the number_format
function instead.
- In
app/helpers.php
replacemoney_format
line withreturn '$'.number_format($price / 100, 2);
- In
app/Product.php
replacemoney_format
line withreturn '$'.number_format($this->price / 100, 2);
- In
config/cart.php
set thethousand_seperator
to an empty string or you might get a 'non well formed numeric value encountered' error. It conflicts withnumber_format
.
Starting from a particular point
If you would like to follow along from a particular point, follow these instructions. I'm going to be starting from my starting point in the first video of the series. You can choose any point by replacing the hash with any particular commit.
- Clone the repo and
cd
into it git checkout f4f651a8a35ebb2ff38ba15771fd65c93051f942
- Follow the rest of the steps above. Instead of
php artisan ecommerce:install
, migrate and seed the normal way withphp artisan migrate --seed
How to build a helper file in laravel project?
you can add a helpers.php file in app folder, open composer.json, and edit the part of autoload. In this case we need to use the helpers.php so we add the new type "file". We want to use the helpers by laravel, so we enter command "composer dumpautoload" to load the autoload part. Because you use the LaravelShoppingcart package, you need to check the price file what type of the price you get by this package. If you want to remove the separator, you need to open cart.php in config folder. In the end of the file, you can find the "'thousand_seperator' => ','" row. It means you would get price like 1,000 but it is a string.
use the stripe-js to build the checkout pages
https://stripe.com/docs/payments/accept-a-payment-charges#web the default cart is 4242 4242 4242 4242 0422 420 some config in services.php file, we need to insert
use the stripe laravel package
https://github.com/cartalyst/stripe-laravel command:composer require cartalyst/stripe-laravel