How to install a php linter (WordPress standards) in Windows 10 and Sublime Text 3

For some time now, I’ve been thinking that I needed to get a php linter in my workflow.

This little addition will decrease debugging time, letting me know instantly if there’s some kind of syntax mistake, and also with WordPress standards active will instruct me to follow all the coding standards.

After a little chat with some friends yesterday night and seeing once again linting in action, I said to myself “Today I’ll get this thing working on Windows”.

After some experiments and reading I’ve encountered the easiest and safest way to do this install, and finally get linting working on my setup (Windows + Sublime Text 3) these are the steps:

Install PHP

  • Download the binaries from http://windows.php.net/download/ (I’ve selected the VC11 x86 Thread Safe version for my setup.)
  • Unzip the downloaded folder to c:\php
  • Activate some basic php modules:
    • Copy php.ini-production to php.ini
    • Uncomment the following lines:
      • extension_dir = “ext”
      • extension=php_bz2.dll
      • extension=php_curl.dll
      • extension=php_fileinfo.dll
      • extension=php_gd2.dll
      • extension=php_intl.dll
      • extension=php_mbstring.dll
      • extension=php_exif.dll
      • extension=php_openssl.dll
      • extension=php_pdo_mysql.dll
      • extension=php_pdo_sqlite.dll

Install Composer

Install PHP Code Sniffer using Composer

  • On your bash console run the following comand:
  • composer global require "squizlabs/php_codesniffer=*"

Install WordPress PHPCS standards

  • On console discover the path to the linter executable
  • which phpcs
  • Go to the PHPCS directory, it should be something like: C:\Users\{your-user}\AppData\Roaming\Composer\vendor\bin\
  • Clone WordPress standards
  • git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
  • Add WordPress to PHPCS configuration

  • phpcs --config-set installed_paths C:\Users\{your-user}\AppData\Roaming\Composer\vendor\bin\wpcs
  • Set the default Standard as WordPress
  • phpcs --config-set default_standard WordPress

Install Linter on Sublime Text 3

The last steps are installing the linter packages on Sublime Text 3, for this open your Package Control and install the following packages:

  1. SublimeLinter
  2. SublimeLinter-phpcs

Next, you’ll need to restart sublime and setup the linter configuration, it’s fairly simple, just copy the file in “Preferences/Package Settings/SublimeLinter/Settings – Default” to “Preferences/Package Settings/SublimeLinter/Settings – User” and edit the phpcs linter configuration setting the standard as WordPress:

"phpcs": {
    "standard": "WordPress"
 },

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

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.