Reader small image

You're reading from  Mastering WooCommerce - Second Edition

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781835085288
Edition2nd Edition
Concepts
Right arrow
Author (1)
Patrick Rauland
Patrick Rauland
author image
Patrick Rauland

Patrick Rauland is obsessed with WooCommerce. He has used it as a customer, worked for WooCommerce support, developed core functionality in WooCommerce itself, led three releases, and helped plan their yearly conference (WooConf). He now helps people by writing his blog, creating courses for LinkedIn Learning/Lynda, and writing books. Patrick is also the co-founder of WooSesh, an online conference for WooCommerce developers and store owners. Patrick lives in Denver, Colorado, where you can probably find him at a local coffee shop, typing away
Read more about Patrick Rauland

Right arrow

Creating Plugins for WooCommerce

Ultimately, WooCommerce is a flexible platform not because of the thousands of existing plugins but because you can code your own plugins. You can customize every single line of WordPress and WooCommerce, which means you can literally change anything. Nothing is impossible. It’s just a question of how much time you want to invest in coding a solution. It’s incredibly powerful for a business.

Becoming a WooCommerce developer could be its own series of books, but we will start by looking at how you can customize a WooCommerce plugin.

We’re going to build a basic WooCommerce plugin, look into building integration with WooCommerce, and then customize the edit product page.

The following topics will be covered in this chapter:

  • Building a basic WooCommerce plugin
  • Customizing order statuses
  • Building a settings page with WooCommerce

By the end of this chapter, you should know the basics of building a plugin...

Technical requirements

This chapter will require you to write code. You should be familiar with PHP code and object-oriented programming to get the most out of this chapter.

The code files for this chapter can be found in the following GitHub repository: https://github.com/PacktPublishing/Mastering-WooCommerce-/tree/main/Chapter13.

Building a basic WooCommerce plugin

To get started, we want to build a plugin that will run when WooCommerce is activated on a site. If WooCommerce is not active, we don’t want our plugin to run, because that’s a waste of processing power.

We’re going to create a plugin and then we’ll configure it to only load when WooCommerce is active. We need to start by creating a plugin file.

Creating a plugin

To get started, we need to create the plugin files. Plugins are typically stored in the WordPress filesystem under wp-content/plugins/{your-plugin}.

There are two ways of creating plugin files:

  • Use a single plugin file that contains the entire plugin
  • Use a folder with multiple plugin files

The vast majority of plugins use multiple files, so we’re going to create a folder for our plugin. Follow the steps given here:

  1. Under /wp-content/plugins/, add a folder for your plugin. I’m going to call mine woocommerce-example...

Customizing order statuses

WooCommerce uses order statuses to track the state of orders. WooCommerce includes several different order statuses, including the following:

  • Completed
  • Processing
  • Pending payment
  • On hold
  • Refunded
  • Canceled
  • Failed

But some shops may need more order statuses. You might need a Building status to indicate that you started building the order. You can easily add a custom order status yourself with some custom code. We’re going to start with our WooCommerce example plugin and then we’re going to register a post status and add it to WooCommerce.

Using the WooCommerce example plugin

We can start by using the example plugin we created earlier in this chapter.

We’ll copy the example code, and then we should change the name of the class from WC_Example to WC_Building_Order_Status, since this describes what this plugin will do. Here’s the code:

// only run if there's no other class with...

Building a settings page with WooCommerce

When you’re building your own plugin, you’ll very likely have to build a settings page. There’s a lot to building a settings page from scratch. But if you are just building an integration, WooCommerce has created some technology that makes it easier for you to add your own settings screen.

We’re going to cover some of the coding decisions in the WooCommerce Integration class. If you want, you can see the finished code athttps://developer.woocommerce.com/docs/creating-custom-settings-for-woocommerce-extensions/.

We can see that one of the plugins we looked at earlier in this book takes advantage of the Integration class, which we can see in the following screenshot:

Figure 13.4: ShipStation uses the WooCommerce Integration class

Figure 13.4: ShipStation uses the WooCommerce Integration class

We’re not going to explain every single line. But we are going to cover the essential lines and why you need them.

Let’s build our own...

Summary

WooCommerce is an incredibly open system where you can do just about anything. We covered some simple tutorials in this chapter to give you a taste of what you can do. You should now know how to build a plugin that only works when WooCommerce is active, how to add new custom order statuses, and how to build an integration with another service.

You’ve taken the first steps to mastering WooCommerce and maybe even developing your own themes and plugins for WooCommerce. In the next chapter, we’ll take a peek into a few advanced topics to become an informed developer who makes safer, more accessible websites.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering WooCommerce - Second Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781835085288
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Patrick Rauland

Patrick Rauland is obsessed with WooCommerce. He has used it as a customer, worked for WooCommerce support, developed core functionality in WooCommerce itself, led three releases, and helped plan their yearly conference (WooConf). He now helps people by writing his blog, creating courses for LinkedIn Learning/Lynda, and writing books. Patrick is also the co-founder of WooSesh, an online conference for WooCommerce developers and store owners. Patrick lives in Denver, Colorado, where you can probably find him at a local coffee shop, typing away
Read more about Patrick Rauland