Blog

Manage your WordPress e-commerce site with Laravel

Manage your WordPress e-commerce site with Laravel
18 Jun 2020 | General | 0

Manage your WordPress e-commerce site with Laravel

Background

Millions of business owners are using WordPress as an e-commerce platform using WooCommerce. I have a love-hate relationship with WordPress when it comes to managing content beyond blogging. One downside of this is that, for every additional feature you need, you have to install a plugin. If none exists in the marketplace, then you don't have a choice but to develop your own. Plus, each plugin comes with many extra features you might not be interested in and constant nagging to upgrade for pro features.

Installing plugins is easy for beginners, but as the business becomes complex, the site is not only slowed down by numerous assets that each plugin brings aboard, but the file system and the database become vast and hard to maintain.

I was recently working on an e-commerce site for a client, and after just a month, the file system was hovering around 3GB and 350MB database files!

The admin back end is not also the prettiest but offers basic functionalities that are sufficient for the primary user. What if you want to create a custom back office without having to develop another plugin?

Laravel to the rescue!

I have been a fan of Laravel since early 2015. When I discovered Laravel, I was then working with CodeIgniter and occasionally, CakePHP, Drupal, or just core PHP. Let me say I have never looked back. Laravel has increased my development time significantly.

In this task, I was to create a Laravel application to

  • Create a back office to manage inventory, and rapidly update the stock quantity of items (the site was also syncing with e-Bay store, to this was a huge time saver for the client)
  • Bulk onboard new parts from a vehicle as it is gets torn down in the shop
  • Quickly and intuitively update other portions of the products as needed and take a product off/online.
  • Perform lossless compression and resizing of product images to increase site speed by >30%
  • Perform automatically scheduled tasks to clean up and optimize both the database and file system
  • Provide staff a clean, easy to use interface that requires almost zero on-boarding.

Fast forward, after requirements, design, and specs were agreed upon, we needed to authenticate on the client's e-commerce WordPress site from Laravel.

Authentication

Let's walk through authenticating a Laravel application against a WordPress database. In another post, I will discuss enforcing roles and permissions and dive deeper into managing the WordPress database, creating Eloquent relationships, and implementing data integrity.

These instructions are for Laravel 7.x.

First, you will need to install and configure jgrossi/corcel package

composer require 'jgrossi/corcel'

php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

This is an awesome package that is build on top of Eloquent ORM

Once installed, head to config/auth.php and switch provider to

'users' => [
'driver' => 'corcel',
'model' => Corcel\Model\User::class,
],

This assumes you will only be using the WordPress database and not 2 databases simultaneously. If that's not the case, check the documentation on the package.

Now head to config/database.php and add a database driver

'wordpress' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'wp_',
    'strict'    => FALSE,
    'engine'    => NULL,
],

Install Laravel auth UI

composer require laravel/ui

Assign the driver to your app/corcel.php to read `

'connection' => 'wordpress',

Good job if you have made it this far! You are essentially done.

Now, if you are working on the e-commerce side of the business like me, you might want to create an Inventory mode and filter only products from your posts with a scope.

function scopeProducts($query){
return $query->where('post_type','product')->where('post_status','!=','auto-draft');
}

In order to query only products from the database, you would call Inventory::products()->get()

Here is the sneak peek of the back office when it was a work-in-progress.

With both authentication and database access in place within Laravel, now you can add any functionality you need to perform in the WP backend with a refreshing flow and design of you like.

Contact me with any questions.

Discussion