| |

How I Created a WooCommerce Plugin Without Knowing Code

The Problem: Restricting Shipping for Certain Products

Probably, you’ve found yourself in a situation where you needed to change a small thing in WordPress to make it work for you. I’ve faced this countless times over the past 14 years of building WordPress websites. The main problem is that I don’t know PHP, and I don’t want to learn it either.

This time, I ran into an issue with WooCommerce. I sell products from Printify or Printful on my website, and some of them only ship to the US. Most of the products do ship everywhere, but some don’t, and I still want to keep them in my store.

I want my website to help people from all over the world. I needed to make a small change: if someone enters a shipping address outside the US, I want the website to notify them and stop them from purchasing the product unless they change the address to the US or remove the problematic product from their cart.

I also want a warning to show up on the product page so they know not to add the product to their cart in the first place. On top of that, I want it to be a very simple setting—just checking a box on the product edit page—so I can let automation handle marking all the products for me instead of doing it manually for each one.

Everything I Added to the Plugin – WooCommerce US Shipping Restriction

Let’s go over everything I added to the plugin, and then I’ll explain exactly how I created it. I have to admit it took me some time because of all the changes I wanted to make. While I was creating the plugin, I kept getting new ideas to make it better, and all the revisions I made added time to the process.

It all starts with the product page. In the general product settings, there’s now a new option to mark if this product ships only to US addresses. That’s all the setup needed on the backend. When I want to update a product, I just check this option, save the changes, and everything else will show up on the front end.

Now, let’s move to the front-end product page. On the product page, there’s a warning that this product ships only to US addresses, along with a US flag icon. I also added the same warning in the catalog view, just below the “Add to Cart” button.

If someone adds the product to their cart and their saved address isn’t in the US, they’ll see a yellow warning message with the text and the US flag. If they continue to the checkout page, the product will still be highlighted in yellow, but now there’s a big delete option, so they can easily remove the product without going back to the cart page. They can also change their address at this stage and proceed with the product.

Additionally, if they try to complete the order with a non-US address, the “Checkout with PayPal” button disappears. This prevents them from completing the order with that product in the cart. If they click to proceed, they’ll get a notification explaining that the product is only available for US addresses, and they can either remove it or update their address.

I didn’t want to prevent the product from being added to the cart if the address wasn’t in the US because sometimes people use services to forward items to their address through a US address, or they might want to send it as a gift to someone in the US. So, I allowed the product to be added to the cart.

That’s everything the plugin does. Now let’s see how I created it—all without knowing any code.

Making the Plugin

I started with a simple instruction to ChatGPT, explaining what I wanted to achieve.

After some back and forth, it finally worked the way I wanted. But then, I decided I also wanted the plugin to allow removing the product directly from the checkout page. Usually, you can only remove products from the cart page, but I didn’t want people to leave the checkout page just to go back and make changes to the cart.

I really hope people outside the US will see the warning before they add the product to their cart. But if they don’t, at least I can make it easier for them to handle it directly on the checkout page.

WordPress Code Types

Since I wanted to add a delete button on the checkout page, I asked ChatGPT to provide the code for that. This required adding a new file for JavaScript code. If you’re not familiar with code like me, you just need to know that WordPress uses a few different types of code like PHP, JavaScript, HTML, etc. When creating a plugin, you’ll often need separate files for each type of code.

When you give ChatGPT instructions for creating a plugin, start by describing the main idea and focus on getting it to work. Once the basic functionality is in place, you can make changes and improvements step by step.

Making Changes to the plugin with ChatGPT

If the code starts getting too long and ChatGPT can’t generate it all at once, ask it to continue from where it stopped. You can copy the output in two or more parts.

When making changes, do each change separately. This way, if something doesn’t work, it’s easier to identify and fix the specific issue.

If ChatGPT provides only part of the code for a change, ask where it should be added so you can place it in the correct location.

Each section of the code usually comes with a title or explanation from ChatGPT. Use these to understand what each part of the code does. If you need to modify something later, you’ll know exactly where to find it in the longer code.

Organizing Code Files for a Functional WordPress Plugin

Now, let’s see how to use the code to build the plugin.

To use the code you got from ChatGPT, open Notepad on your computer. Copy the code from ChatGPT and paste it into Notepad. You can ask ChatGPT to give you a name for the plugin you’re creating and use that as the file name. Save the file in a new folder with the same name as the plugin.

If the file is PHP, you need to save it with a .php extension. To ensure it saves correctly, choose the file type All Files when saving. If you don’t select this option, it won’t save correctly.

If the code also includes JavaScript, save it in a separate file within the same folder. Use a .js extension for the JavaScript file and, again, choose All Files when saving. You can ask ChatGPT for the exact names for all the files.

Once you’ve saved all the files, exit the folder and right-click on it. You should see an option to compress the folder. Click on it to create a ZIP file. This ZIP file is your plugin.

Testing and Refining the Plugin for Perfect Results

Now, go to your WordPress website. In the backend, click on Plugins, then Add New, and upload the ZIP file from your computer. If the plugin is good, it will install without issues.

If you encounter an error and your website goes down because of it, you’ll need to access your FTP panel. Find the wp-content folder, then the plugins folder, and locate the plugin folder you created. Delete it from there.

After that, return to ChatGPT, explain the issue, and ask for help finding and fixing the problem.

If you make any changes, update the PHP file or the JavaScript file as needed and save it again. Make sure to delete the previous compressed file to avoid mistakes when uploading. Then compress the folder again into a ZIP file. Keep testing until everything works exactly as you want it to.

Automating Product Updates with TaskMagic for WooCommerce

 I’ve found for the uploading process: when you upload products from Printify, Printful, or any other service connected to your WooCommerce store, you’re limited by what the service allows during the upload process. Sometimes, you can choose categories, and sometimes you can’t. Either way, you usually still need to go into the products you’ve uploaded and make changes.

I have another tutorial on how to automate this process using TaskMagic automation. If you have TaskMagic and want to go through all your products to update them, you can add a step to your existing automation for editing WooCommerce products.

In the automation, you can add a step to click on the checkbox that marks the product as not shipping outside the US. It doesn’t matter where you place this step in the workflow—just add it between the existing steps for updating products.

Get Your Free WooCommerce Plugin

get free plugin – woocommerce us only shipping

Similar Posts