4 Useful WooCommerce Functions

A fast and reliable way to change WooCommerce behaviours

While working on WooCommerce websites, I’ve found that I was using (most of the times) 4 simple functions that allowed me to have a superior control on the created e-commerce solution.

 

In this post you’ll find functions for:

  • Add WooCommerce CSS classes to any template you want
  • Change WooCommerce products per page and product columns on listings
  • Add BCC to all emails sent by WooCommerce
  • Remove WooCommerce hooked actions and create your own function with it

 

Add WooCommerce CSS classes to a page template or a single template from custom post type


add_filter('body_class','add_this_classes');

function add_this_classes($classes) {
        //if is single from CPT "post_type" or page template "template.php"
	if(is_singular('post_type') || is_page_template('template.php')){

		// add 'class-name' to the $classes array
		$classes[] = 'woocommerce';
		$classes[] = 'woocommerce-page';

	}

	// return the $classes array
	return $classes;
}

 

Change WooCommerce products per page and columns


//Change products per page to 20
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 20;' ), 20 );
add_filter('loop_shop_columns', 'loop_columns');

if (!function_exists('loop_columns')) {

	function loop_columns() {

		return 8; //change to 8 columns

	}

}

 

Add BCC in all emails sent by WooCommerce


add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2);

function add_bcc_all_emails($headers, $object) {

	$headers = array();
	$headers[] = 'Bcc: Name <me@email.com>';
	$headers[] = 'Content-Type: text/html';

	return $headers;
}

 

Remove WooCommerce hooked action and use the function from it

//Remove add to cart from single product summary
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

woocommerce_template_single_add_to_cart();

WooCommerce – Automatically set order status after payment is received

This post has an updated version on WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

Using Filter: woocommerce_payment_complete_order_status

Sometimes when you’re developing an e-commerce website you’ll need to automatically check the payment status and mark an order as completed.

This usually implies that you’re selling a “virtual good” but not always, I’ve been developing a store that sells “virtual goods” that aren’t marked as such in WooCommerce due to some internal functionalities.

WooCommerce does this behavior automatically on virtual goods but not in general, also I’ve other payment methods that have a callback and I need this behavior in those as well.

To achieve this, I’ve used the function:

add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );

function rfvc_update_order_status( $order_status, $order_id ) {

 $order = new WC_Order( $order_id );

 if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {

 return 'completed';

 }

 return $order_status;
}

This function does 2 simple things, first using the filter it’s hooked to the payment status change to complete, when it happens, it checks the order status, and if it has changed to “processing” and has previously set as “on-hold”, “pending” or “failed” the new status will be “completed”, allowing all the actions set to this status to occur automatically.

Using this hook will allow you to set this behavior to all payment methods at once, but if you wish you could also check the payment method from the order, and filter the change accordingly.

Creating a web and WordPress development environment

Installing Ubuntu Server Virtual Machine, using VMware (Part 2)

After installing Ubuntu Server, using VMWare as explained in my previous post, You’ll have a local development environment for your projects that’s fully functional, but only with  the basic features.

You’ll want to use a series of tools in your development stack, that aren’t available from start in the installation and also, have the means to easily edit the files inside your server, preferably without the need to use FTP or other tools, after all, this is your local stack.

In this post I’ll explain how I’ve configured the server to improve the development environment and run the following tools correctly,

  • Samba ( File sharing between the virtual machine and our local system)
  • Git
  • Ruby and Gem
  • SASS
  • Bourbon
  • node.js
  • Grunt
  • Wordmove

1. Install and configure Samba on Ubuntu VM

First step, the obvious, install Samba using root access

sudo apt-get install samba

Next you’ll need to open your Samba configuration file ( /etc/samba/smb.conf ) and add a few lines in order to configure a shared folder, start by opening your configuration file with your favorite editor and then you’ll need to add the following lines to your file:

# The following property ensures that existing files do not have their permissions
# reset to the "create mask" (defined below) if they are changed
 map archive = no

# Notify upon file changes so that Windows can detect such changes
 change notify = yes

[htdocs]
 comment = Htdocs Files
 path = /opt/lampp/htdocs/
 guest ok = no
 browseable = yes
 writable = yes
 create mask = 0664
 directory mask = 0775

In the above, I’m assuming you want to share the htdocs folder from your lampp install.

Next, you need to add a Samba user, with the same name and password as your Ubuntu user account


sudo smbpasswd *username*

To finish this step you need to restart the samba service,


sudo service smbd restart

2. Setting permissions on htdocs folder

In order to be able to edit, add and delete files from the htdocs folder you’ll need to set all the permissions correctly on that folder, to do this you’ll need to change the folder permissions, doing the following:

First you need to create a new group “www” which will have the permissions for this folder,

sudo groupadd www

Next, you’ll change the group on the directory,

sudo chgrp -R www /opt/lampp/htdocs

and then you need to set the correct permissions on that directory:

sudo chmod 2775 /opt/lampp/htdocs

To finalize, add your user to this group, running the following command:

sudo usermod -aG www *username*

Now you’ll need to logout or restart your server, in order to these changes take effect, as these rules are read at login time.

3. Configure static IP in your VM and change your windows “hosts” file

For me this is one of the most important steps on this configuration, I love being able to type something like local.dev in my browser, putty or ftp and be able to access my VM without any hassle.

To do this you need to change two files in your VM.

First, open the “interfaces” file ( etc/network/interfaces ) in your favorite editor, and change the following lines:

auto eth0
iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.2
broadcast 192.168.0.255

You’ll need to get the correct values for each from your own network configuration, just have in mind that the ip address need to be unique and in the same network as your local machine.

Next, open the “resolve” configuration file ( etc/resolv.conf ) and change the nameserver to match your gateway ip address.

Then just restart the network, by running the following command:

sudo etc/init.d/networking restart

Back in windows you need to add a new entry to your host file, for that you need to open notepad or your favorite text editor, as administrator, then navigate to the host file, usually in: C:\Windows\System32\drivers\etc and add the following line to your file:

192.168.1.100 local.dev

The first part is your virtual machine ip address, the second your desired alias for it.

4. Install all the packages you want

Now it’s time to install all the packages you want in your VM, before selecting them and start installing, just run an update to make sure you’re getting the latest stable versions.

sudo apt-get update

Now it’s time for installing the packages, usually I install the following:

  • Git
  • Ruby
  • Gem
  • Sass
  • Bourbon
  • Wordmove
  • lftp
  • mysqldump
  • grunt
  • Node.js

Please have in mind that these are the steps I use to create a local VM for development, these configurations shouldn’t be used on a VPS or a production environment.

There are other alternative configurations for a local environment including the famous VVV, https://github.com/Varying-Vagrant-Vagrants/VVV . The last time I tried to install this on my windows laptop I couldn’t get it to work correctly due to some windows permissions, I’ll give it another try some time soon, but for now I’m sticking with my own VM.