Gerhard Potgieter

Senior Software Engineer @ Automattic specializing in eCommerce

WooCommerce 3.3: Hide uncategorize category from shop — February 9, 2018

WooCommerce 3.3: Hide uncategorize category from shop

In WooCommerce 3.3 we introduced a new uncategorized category all product will default to when no other category is assigned to them. This is the same behavior that WordPress uses for posts. With that, unfortunately, if your shop is set up to display categories on the shop page then this new uncategorized category will also show up.

Although we give you the possibility to rename the category to whatever you would like, there might be cases where you would like to rather not display this on your shop page. To do that you can simply add the following piece of code to your site via your theme’s functions.php file, or via a custom plugin. Remember to not include the opening tag<?php if you are adding this to an existing file.


<?php
add_filter( 'woocommerce_product_subcategories_args', 'remove_uncategorized_category' );
/**
* Remove uncategorized category from shop page.
*
* @param array $args Current arguments.
* @return array
**/
function remove_uncategorized_category( $args ) {
$uncategorized = get_option( 'default_product_cat' );
$args['exclude'] = $uncategorized;
return $args;
}

WooCommerce 3.2: Add back option to resend admin order email — October 13, 2017

WooCommerce 3.2: Add back option to resend admin order email

Since WooCommerce 3.2 the option to resend the admin new order email was removed. The reason was that it was a pretty much-unused feature as admins already have access to the orders so they could just look it up.

However, if you relied on this feature for some reason and now all of sudden can’t use it, fear not as it can easily be added back with the following snippet.

As always include the following code in your theme’s functions.php file omitting the opening <?php tags


<?php
/**
* This adds back an option to resend the admin new order email from the order edit screen that was removed in WooCommerce 3.2
*/
/**
* Filter to add a new menu to the dropdown
*
* @param array $actions
* @return array
*/
function add_resend_admin_order_email_action( $actions ) {
$actions['send_admin_order_details'] = __( 'Email order details to Admin', 'woocommerce' );
return $actions;
}
add_filter( 'woocommerce_order_actions', 'add_resend_admin_order_email_action', 10, 1 );
/**
* Hook into newly added send_admin_order_details to handle sending of admin emails
*
* @param $order Order object
* @return void
*/
function resend_send_admin_order_email( $order ) {
// Send the admin new order email.
WC()->payment_gateways();
WC()->shipping();
WC()->mailer()->emails['WC_Email_New_Order']->trigger( $order->get_id(), $order );
// Note the event.
$order->add_order_note( __( 'Order details manually sent to admin.', 'woocommerce' ), false, true );
do_action( 'woocommerce_after_resend_order_email', $order, 'new_order' );
// Change the post saved message.
add_filter( 'redirect_post_location', array( 'WC_Meta_Box_Order_Actions', 'set_email_sent_message' ) );
}
add_action( 'woocommerce_order_action_send_admin_order_details', 'resend_send_admin_order_email', 10, 1 );

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce: Hide refund reason from customers — October 9, 2017

WooCommerce: Hide refund reason from customers

Refunds can be a sore subject but it is something that every store has to deal with, keeping track of refund reasons can be very beneficial to you so you can keep pushing your refund rate down further and further.

The problem is that you do not always want your customer to see the reason you enter for when doing a refund, which WooCommerce will display by default to each customer.

Good news is that you can easily replace the refund reason in your customer-facing emails and pages with static text like just “Refund”.

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


<?php
/**
* Replace refund reason with generic Refund text.
*
* @param $total_rows
* @param $order
* @param $tax_display
* @return array
*/
function remove_public_facing_refund_reason( $total_rows, $order, $tax_display ) {
foreach ( $total_rows as $id => $row ) {
if ( false !== stripos( $id, 'refund_' ) ) {
$total_rows[ $id ]['label'] = __( 'Refund' );
}
}
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'remove_public_facing_refund_reason', 10, 3 );

view raw

functions.php

hosted with ❤ by GitHub

 

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 sharklashers.com 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


<?php
/**
* Do not allow account creation with temp email addresses
* @param Object $validation_errors
* @param string $username
* @param string $email
* @return WP_Error
*/
function do_not_allow_temp_email_addresses( $validation_errors, $username, $email ) {
$prohibitied_domains = array(
'sharklasers.com',
'grr.la',
'guerrillamail.biz',
'guerrillamail.com',
'guerrillamail.de',
'guerrillamail.net',
'guerrillamail.org',
'guerrillamailblock.com',
'spam4.me',
);
$email_domain = explode( '@', $email )[1];
if ( in_array( $email_domain, $prohibitied_domains ) ) {
return new WP_Error( 'registration-error-bad-email', __( 'Please use a valid email address.' ) );
}
return $validation_errors;
} // End do_not_allow_temp_email_addresses()
add_filter( 'woocommerce_registration_errors', 'do_not_allow_temp_email_addresses', 10, 3 );

view raw

fake-emails.php

hosted with ❤ by GitHub

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


<?php
// place the following code in your theme's functions.php file
// Add a second password field to the checkout page.
add_action( 'woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1 );
function wc_add_confirm_password_checkout( $checkout ) {
if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
$checkout->checkout_fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __( 'Confirm password', 'woocommerce' ),
'required' => true,
'placeholder' => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
);
}
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
if ( strcmp( $posted['account_password'], $posted['account_password2'] ) !== 0 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
}
}
?>

view raw

functions.php

hosted with ❤ by GitHub

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


<?php
// Place in your theme's functions.php file
// Revert grouped product prices to WooCommerce 2.0 format
add_filter( 'woocommerce_grouped_price_html', 'wc_wc20_grouped_price_format', 10, 2 );
function wc_wc20_grouped_price_format( $price, $product ) {
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
$get_price_method = 'get_price_' . $tax_display_mode . 'uding_tax';
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if ( $min_price == $max_price ) {
$display_price = wc_price( $product->$get_price_method( 1, $min_price ) );
} else {
$from = wc_price( $product->$get_price_method( 1, $min_price ) );
$display_price = sprintf( __( 'From: %1$s', 'woocommerce' ), $from );
}
return $display_price;
}

view raw

functions.php

hosted with ❤ by GitHub

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.

%d bloggers like this: