Gerhard Potgieter

Senior Software Engineer @ Automattic specializing in eCommerce

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


<?php
// Show trailing zeros on prices, default is to hide it.
add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
// set to false to show trailing zeros
return false;
}
?>

view raw

functions.php

hosted with ❤ by GitHub

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


/**
* Use WC 2.0 variable price format, now include sale price strikeout
*
* @param string $price
* @param object $product
* @return string
*/
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );

view raw

functions.php

hosted with ❤ by GitHub

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.


<?php
// Include the client library
require_once 'class-wc-api-client.php';
$consumer_key = 'ck_fcedaba8f0fcb0fb4ae4f1211a75da72'; // Add your own Consumer Key here
$consumer_secret = 'cs_9914968ae9adafd3741c818bf6d704c7'; // Add your own Consumer Secret here
$store_url = 'http://localhost/&#39;; // Add the home URL to the store you want to connect to here
// Initialize the class
$wc_api = new WC_API_Client( $consumer_key, $consumer_secret, $store_url );
?>

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.


<?php
// Get all orders
$orders = $wc_api->get_orders();
// Output the order object retrieved from the API
print_r( $orders );
?>

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 README.md 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.


<?php
// remove product description from single product pages
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_filter( 'woocommerce_product_tabs', 'wc_remove_description_tab', 11, 1 );
function wc_remove_description_tab( $tabs ) {
if ( isset( $tabs['description'] ) ) {
unset( $tabs['description'] );
}
}
?>

view raw

functions.php

hosted with ❤ by GitHub

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.


<?php
// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values['quantity'];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}
// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
$multiples = 6;
$class = 'bottle';
$total_products = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = get_product( $values['product_id'] );
if ( $product->get_shipping_class() == $class ) {
$total_products += $values['quantity'];
}
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' );
}
?>

view raw

gistfile1.php

hosted with ❤ by GitHub

WooCommerce Add Shipping Method to Emails — December 5, 2013

WooCommerce Add Shipping Method to Emails

Following up on the post about adding the Payment Type to your WooCommerce emails, a reader asked how they would be able to also add the Shipping Method to the WooCommerce emails.

Again WooCommerce does add a lot of information to the emails that goes out to the customers and admin but there are some things that are not part of the emails and another one of those things are the Shipping Method.

The shipping method could useful to display in emails to let shop admin knows how to ship the order if these emails are being used as packaging slips, or to let the customer know what type of shipping they selected at checkout.

To add the shipping method to all WooCommerce emails or just add it to the admin emails add the following code to your theme’s functions.php file.


<?php
// Place the following code in your theme's functions.php file to add the shipping method to all emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_emails', 15, 2 );
function wc_add_shipping_method_to_emails( $order, $is_admin_email ) {
echo '<p><strong>Shipping Method:</strong> ' . $order->get_shipping_method() . '</p>';
}
// Place the following code in your theme's functions.php file to add the shipping methid to admin emails only
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_admin_emails', 15, 2 );
function wc_add_shipping_method_to_admin_emails( $order, $is_admin_email ) {
if ( $is_admin_email ) {
echo '<p><strong>Shipping Method:</strong> ' . $order->get_shipping_method() . '</p>';
}
}
?>

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce Add Payment Type to Emails — November 14, 2013

WooCommerce Add Payment Type to Emails

WooCommerce send customers and store admins emails when new orders are placed, these emails contains all sorts of info relating to the order, however it is missing a couple of things that can be useful to customers or store admins.

One of the fields that is not present in the WooCommerce order emails are the payment type, some WooCommerce store admins would perhaps want to know this data in emails for decisions on whether to ship items immediately or to hold off for a while for money to clear.

To add the payment type to all WooCommerce emails or just admin email add the following code to your theme’s functions.php file, this is upgrade safe and the changes will stay in place after WooCommerce updates.


<?php
// Place the following code in your theme's functions.php file to add the payment type to all emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_payment_type_to_emails', 15, 2 );
function wc_add_payment_type_to_emails( $order, $is_admin_email ) {
echo '<p><strong>Payment Type:</strong> ' . $order->payment_method_title . '</p>';
}
// Place the following code in your theme's functions.php file to add the payment type to admin emails only
add_action( 'woocommerce_email_after_order_table', 'wc_add_payment_type_to_admin_emails', 15, 2 );
function wc_add_payment_type_to_admin_emails( $order, $is_admin_email ) {
if ( $is_admin_email ) {
echo '<p><strong>Payment Type:</strong> ' . $order->payment_method_title . '</p>';
}
}
?>

view raw

functions.php

hosted with ❤ by GitHub

Controlling WordPress Auto Updates — October 25, 2013

Controlling WordPress Auto Updates

WordPress 3.7 was just released and one major feature that it includes is the ability have WordPress automatically updated to new versions without you having to log in and click a button.

I think it is absolutely great feature and can save you so much time in having to not log into all your site and clicking buttons, however it may not to appeal to everyone.

Reliability wise the auto update feature has been tested to death in beta and release candidates and the team working on this has ensured to cover all failure points to ensure that should something go wrong it will roll back to your previous version of WordPress. Even after 25000 auto updates in the RC releases there was not one single failure.

The WordPress team has catered for controlling auto updates via an array of filters and constants, the main constant being WP_AUTO_UPDATE_CORE. With the WP_AUTO_UPDATE_CORE constant you can control to allow disabling of auto updates, only allowing minor updates to occur automatically or allowing all updates to be done automatically.

The code below shows you how to control the automatic updates for different scenarios, you will place the code in your wp-config.php file.


// Enable Automatic Updates for all updates
define( 'WP_AUTO_UPDATE_CORE', true );
// Disable Automatic Updates for all updates
define( 'WP_AUTO_UPDATE_CORE', false );
// Only allow Automatic Updates for minor updates
define( 'WP_AUTO_UPDATE_CORE', 'minor' );

view raw

wp-config.php

hosted with ❤ by GitHub

If you want more control of specific features such as allowing auto updates of development releases such as beta and alpha releases, or want to only enable just major or just minor updates you can do that with the use of some filters. The following code shows examples of how to control specific WordPress auto update features, this code needs to be placed in your theme’s functions.php file.


//Allow auto updates of development releases ie alpha, beta and RC
add_filter( 'allow_dev_auto_core_updates', 'wp_control_dev_auto_updates' );
function wp_control_dev_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
//Allow auto updates of minor releases ie 3.7.1, 3.7.2
add_filter( 'allow_minor_auto_core_updates', 'wp_control_minor_auto_updates' );
function wp_control_minor_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
//Allow auto updates of major releases ie 3.7, 3.8, 3.9
add_filter( 'allow_major_auto_core_updates', 'wp_control_major_auto_updates' );
function wp_control_major_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
// Allow auto theme updates
add_filter( 'auto_update_theme', 'wp_control_theme_auto_updates' );
function function wp_control_theme_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
// Allow auto plugin updates
add_filter( 'auto_update_plugin', 'wp_control_plugin_auto_updates' );
function function wp_control_plugin_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}
// Allow auto language updates
add_filter( 'auto_update_translation', 'wp_control_translation_auto_updates' );
function function wp_control_translation_auto_updates( $value ) {
// return true to enable and false to disable
return true;
}

view raw

functions.php

hosted with ❤ by GitHub

If you have any questions or suggestions please feel free to leave them in the comments.

WooCommerce Number Of Products Sold — October 14, 2013

WooCommerce Number Of Products Sold

A question that has been popping up quite a lot lately in the WooCommerce support forums is how do I display the total amount of units sold for a product on the product page. In this WooCommerce tutorial I will be showing you how to display this on the product page.

Luckily WooCommerce already takes care of the majority of work for us to do this as it automatically keeps track of the number of units sold for each product for reporting purpose, all we have to do it do retrieve the value and display it on the product page.

To display the number of units sold for a product add the following snippet of code to your theme’s functions.php file


<?php
add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );
function wc_product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, 'total_sales', true );
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
?>

This will result in the Units Sold text to display underneath the product title and price

WooCommerce number of units sold

WordCamp Cape Town 2013 — September 23, 2013

WordCamp Cape Town 2013

WordCamp Cape Town 2013

At the beginning of the year our team at WooThemes HQ down here in Cape Town decided to approach previous organisers of the WordCamp Cape Town event to hear what their plans was for this year and if they would be willing to give over reigns of the event to us. Without any hassle or negativity from their side they agreed and we immediately set out to start promoting WordPress in South Africa more.

We started the WPCapeTown site, which is our main site and body under which we promote WordPress. The thing to note here is that even though it is a Cape Town community it is not limited to Cape Town and we welcome any WordPress user/creator from all over South Africa.

After two very successful events during the year we are pleased to announce that this year’s WordCamp Cape Town conference is a go. WordCamp Cape Town 2013 will take place at the beautiful Cape Town Stadium on 7 November 2013 and will cater for 400 of South Africa’s WordPress small business owners, non-profit agencies, realtors, photographers, designers, bloggers, journalists and web developers

Tickets for the event went on sale Friday 20 September and all discounted early bird tickets sold out in a matter of a couple of hours, with only general tickets left at R350 each.

If you are unfamiliar with WordCamps, they are community-organised events, championed by WordPress users, for WordPress users. The non-profit event will bring together WordPress enthusiasts from across South Africa to share best practices and strategies for WordPress development. Attendees range from casual users to advanced development professionals. All attendees will have the opportunity to attend a variety of sessions, presented by industry professionals, to share ideas and get to know each other.

The speaker list for this year’s WordCamp Cape Town are still in the finalisation stages but you can be assured that it will include top local and international WordPress professionals. This years theme for WordCamp Cape Town is “The Business Of WordPress”.

A WordCamp Cape Town 2013 ticket will give you access to 8 or more sessions as well as lunch, coffee, snacks, secure parking in the stadium, a free WordCamp Cape Town T-shirt and entry to the after party. Additional activities, workshops and networking events will be announced in the weeks leading up to the event.

So what are you waiting for, grab your WordCamp Cape Town 2013 Tickets before they are sold out as this a WordPress event you cannot afford to miss out on.

%d bloggers like this: