Gerhard Potgieter

WordPress / WooCommerce Engineer at Automattic

WooCommerce: Do not allow account creation with fake emails — November 16, 2016

WooCommerce: Do not allow account creation with fake emails

ECommerce is serious business, that is why when a customer signs up to your store you want the most accurate information to stay in touch, promote your products and offer decent after sales service. Unfortunately, there are lots of chancers out there on the internet that use fraudulent cards and fake emails to purchase products from your store, which can leave you to foot the bill.

The following piece of code will prohibit usage of fake emails for creating accounts, it focuses on but can easily be expanded to include other email domains by just adding to the array.

Add the following code to end of your theme’s functions.php file omitting the opening <?php¬†tag

Uploading Files With wp_remote_post Or wp_remote_request — July 30, 2014

Uploading Files With wp_remote_post Or wp_remote_request

My job at WooThemes enables me to work on all sorts of cool projects and enables me to push the limits of what is possible with¬†WordPress each day. Some of the projects I work on that I enjoy the most is projects that involves interfacing with any sort of API service, I am a sucker for an API ūüôā

In a recent project which involved interfacing with an API service I had to upload a file to the service, and wanting to stay true to the WordPress way I naturally built the whole API interface layer using native WordPress functionality. This involved making use of the wp_remote_request function, which is called by the wrapper function wp_remote_post to send through POST requests.

After Googling for quite a while I just could not get any examples of uploading files in binary using wp_remote_post or wp_remote_request, for that matter, which resulted in me having to figure this out on my own.

The API document was straight forward, in order to upload a file you need to post it as binary to API endpoint, now there are plenty of example of doing this using cURL, so I used that as a starting point and converted it to WordPress native functionality.

The cURL way of uploading the file

Digging through some cURL docs I found that I basically needed to set a header to binary, and then stream the file binary content as the body, and found the following way of uploading the file using either wp_remote_request or wp_remote_post

The above code shows examples of using either wp_remote_request or wp_remote_post, as you can see there is not much difference between the two, instead that with wp_remote_post there is no need to set the method header as it is a wrapper function that does that automatically.

In some cases API services will require you to do uploads with PUT requests instead of POST, in that case you will use the wp_remote_request function and just set the method header to PUT instead of POST to make a PUT request.

Hope this helps someone out there that has also struggled to do this using native WordPress functions.




WooCommerce 2.1 Add Confirm Password Field at Checkout — February 25, 2014

WooCommerce 2.1 Add Confirm Password Field at Checkout

WooCommerce 2.1 Confirm Password Field on CheckoutThe last couple of weeks I have spent a lot of time working on some tutorials to reverse some of the changes introduced in WooCommerce 2.1 to the ways it was in WooCommerce 2.0.

This tutorial is another one of this cases, WooCommerce 2.1 removed the password confirm field and functionality from the checkout page as it was thought that should a customer make a typo in the password field they can easily just reset it via the password reset functionality in WooCommerce.

However if you would still like add this password confirm field to your WooCommerce 2.1 checkout page, good news is this is still possible.

The code below will add an additional field underneath your password field on the checkout page called Confirm Password and when the customer places the order it will check the two password field against each other and give an error message and prohibit checkout if they do not match.

Place the code below in your theme’s functions.php file

WooCommerce 2.1 Grouped Prices Revert To WooCommerce 2.0 Format — February 24, 2014

WooCommerce 2.1 Grouped Prices Revert To WooCommerce 2.0 Format

WooCommerce 2.1 Grouped Product Prices, Revert to WooCommerce 2.0 Format


Last week I did a tutorial that showed you how to change your WooCommerce 2.1 Variation prices from the new range format to the old WooCommerce 2.0 From: format, that tutorial however only included changing the prices of variable products.

The following tutorial shows you how to modify the Grouped Product prices that also uses the new range format in WooCommerce 2.1 to the WooCommerce 2.0 From: format.

To change the pricing format for your¬†WooCommerce grouped products add the code below to your theme’s functions.php file

WooCommerce Show Trailing Zeros on Prices — February 21, 2014

WooCommerce Show Trailing Zeros on Prices

WooCommerce Show Trailing Zeros on Prices

One of the changes that was made with the refinement of the WooCommerce Settings in WooCommerce 2.1 was the removal of the option to show trailing zeros after prices.

Pre WooCommerce 2.1 there was a checkbox you could check to show the prices with trailing zeros, this was removed and replaced with a filter instead.

In order to display trailing zeros on your prices add the code below to your theme’s functions.php file

WooCommerce 2.1 Variation Prices Revert To 2.0 Format — February 13, 2014

WooCommerce 2.1 Variation Prices Revert To 2.0 Format

WooCommerce 2.1 change price range to WooCommerce 2.0 From priceWith WooCommerce 2.1 just being released a couple of days ago, a lot of users may have noticed a couple of big changes to the plugin, like a refined settings section and the introduction of an all new REST API.

The aim with each major WooCommerce release is to simplify and make it faster and more scalable, and with WooCommerce 2.1 this meant that a lot of the setting that was rarely uses was removed and a few formatting changes was made based on customer feedback.

One of the formatting changes that was made was to remove the “From: $x” price formatting of variation products in favor of a range ie “$x – $y”. This new range format for variable product may not appeal to everyone and that is where the following snippet comes into play.

The code snippet will change the new range price format back to the “From:” price format that users are accustomed to in WooCommerce 2.0.

To revert your¬†WooCommerce variation prices back to the “From:” price format add the following code to your theme’s functions.php file

WooCommerce REST API Client Library — February 10, 2014

WooCommerce REST API Client Library

WooCommerce REST API Client Library

With WooCommerce 2.1 now out and about, it brings with it an array of new features and changes. One of these new features is the all new WooCommerce REST API.

The WooCommerce REST API comes bundled with WooCommerce 2.1+ and allows you all sorts of API calls to interact with your WooCommerce store data. You can get access to your Order, Customer, Coupon, Product and Reporting data all through the API.

Having it be a new API interacting with it can sometimes be a difficult task, that is why most software with API’s offer some sort of API Client Library which makes interacting with the API a breeze.

I have been spending quite a lot of time playing around and testing the API since its first commit into WooCommerce core and due to this built quite an easy to use and extendable API Client Library that I used to test and later on refined to make available to the public for use.

The WooCommerce REST API has two authentication methods, one legged oAuth 1.1, and Basic HTTP authentication, the method being used is all dependent on whether your WooCommerce store has a valid SSL certificate and if you have secure checkout enabled. I have developed the WooCommerce REST API Client Library to support both methods as the WooCommerce REST API require you to use oAuth when you have no valid SSL certificate and Basic authentication when you have secure checkout enabled with a valid SSL certificate.

Enabling the WooCommerce REST API

In order to enable the WooCommerce REST API and to start using it you will need to enable it in your WooCommerce settings.

This can be done by going to WooCommerce -> Settings -> General tab in the WordPress admin area, and then making sure the Enable the REST API option is checked. This should be checked by default.

Generating API Keys

In order to access data through the WooCommerce REST API you will require a Consumer Key and Secret. This is used to authenticate the API calls and to ensure that the call being made can access the data it is trying to access.

API Consumer Keys and Secrets are tied to users, which allows WooCommerce to restrict access to certain data based on the user role, so for instance if you set up the user to only have access to products and they try to access orders with their API details they will not be able to.

To generate API details head to either Users -> All Users or Users -> Your Profile in the WordPress admin area. If you followed the Users -> All Users path you will need to search for the user you want to generate API credentials for and edit it.

Once you are on the edit profile page, scroll down to the bottom of the page, there should be a checkbox to generate API Keys, check this and save the profile. Once the page refreshes, if you scroll down again there will be a Consumer Key and Consumer Password listed, and if you are a site admin you will also have the option to select read/write access.

Communicating with the WooCommerce REST API

Once you have the Consumer Key and Consumer Secret you are now ready to start interacting with the WooCommerce REST API.

This is where the WooCommerce REST API Client Library comes into play, you can now head to the WooCommerce REST API Client Library page on GitHub and download the PHP client library from there.

Once you have downloaded the PHP client library you will find a couple of file, the important one here is class-wc-api-client.php, this is the file you will need to use to be able to make use of the client library and make calls to the WooCommerce REST API. There is also a folder called example which shows you how to interact with the client library in order to make calls to your WooCommerce store.

Using the WooCommerce REST API Client Library

If the example code provided with the client library is not enough and the documentation listed on the GitHub page does not explain it well enough here is a quick intro on how to get started using the client library with the WooCommerce REST API.

Connecting to the REST API
In order to connect to the REST API of your store you will need to have the URL to your store as well as your Consumer Key and Consumer Secret ready. Use the following PHP code to include the client library and access the WooCommerce REST API of your store. Be sure to use https if you have secure checkout enabled on your site.

Making a call to the REST API
Once the client library has been initiated you can use the api object to make calls to the WooCommerce REST API, all the calls will return the data in JSON decoded format.

And that is it, for more documentation on all the available function calls, and how to call custom API endpoints added through extensions etc please see the file on the GitHub page

Contributions are welcome, if you spot a bug or would like to add an enhancement feel free to fork and send a pull request.

WooCommerce Remove Product Description From Single Product Page — January 24, 2014

WooCommerce Remove Product Description From Single Product Page

WooCommerce Remove Product Descriptions

There are some cases where you would like to have you WooCommerce products not display a description, this tutorial will help you achieve just that.

By default WooCommerce has two description fields, a short description and a long description. The short description is usually an excerpt take from the long description unless you enter your own short description on the product page.

The short description is displayed right next to the image on the product page underneath the title, where as the long description is displayed in a tab at the bottom of the product page.

If you would like to either remove the¬†WooCommerce short description or the long description tab, or both, you can do so by adding the following code to your theme’s functions.php file.

WooCommerce Allow Checkout in Multiples Only — December 10, 2013

WooCommerce Allow Checkout in Multiples Only

WooCommerce Limit Checkout To Multiples Of a Product

Say you operate a WooCommerce store where you sell products that are shipped in boxes but want customers to make up their own boxes with different products, by default WooCommerce will only allow you to sell products in the the quantities you set the product up with and the customer will be able to check out with any amount of items in the cart.

For instance if you operate a online wine store with WooCommerce and would like to sell your wine by the bottle but only want customers to checkout if they have quantities selected that would make up a whole box, the following tutorial is for you.

With the code below you can setup your products that each product is a single bottle of wine and then force the customer to add multiples of any 6 products to the cart before they would be able to checkout. If the customer for example adds only 5 bottles to the cart and then tries to checkout they will be presented with a message to order in multiples of 6.

You can even take it a step further and only allow the rule to apply to products with a certain shipping class, this will allow you to sell single bottles as well as cases without having the multiples rule apply to the case products.

To force the customer to add multiples of a certain quantity to the cart before being able to checkout add the following code to your theme’s functions.php file.

%d bloggers like this: