WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

This post is an update onWooCommerce – Automatically set order status after payment is received

WooCommerce is a great option to sell all type of goods, being it virtual or physical products you’ll quickly add them to your store and start selling to your visitors.

By default when WooCommerce system processes a payment it’ll set an order with only virtual and downloadable products to completed but sometimes this isn’t enough for your store.

In this case, the store sells physical products and subscriptions, that can be a physical product sent by mail each month or a virtual product that gives you access to restricted areas of the website, because of that we need to change WooCommerce default behavior and automatically set our virtual subscriptions as completed orders, after the payment is complete.

For doing this we’re going to use a hook “woocommerce_order_item_needs_processing” and check if the products on the order are only virtual and subscriptions.

/**
 * Set Orders from Subscriptions marked as Virtual Products Completed Automatically.
 *
 * @param  boolean $virtual_downloadable_item If item is virtual and downloadable.
 * @param  Object  $_product                  Product Object.
 * @param  int     $product_id                Product ID.
 * @return Boolean                            Should not be set to processing.
 */
function rfvc_set_virtual_subscriptions_completed( $virtual_downloadable_item, $_product, $product_id ) {

	if ( $_product->is_virtual() && is_a( $_product, 'WC_Product_Subscription' ) ) {
		$virtual_downloadable_item = true;
	}

	return $virtual_downloadable_item;
}
add_filter( 'woocommerce_order_item_needs_processing', 'rfvc_set_virtual_subscriptions_completed', 10, 3 );

You can also use this code to change the order status in virtual products, without them being subscriptions, removing the check for a subscription product object && is_a( $_product, 'WC_Product_Subscription' )

What WordPress and a broken car have in common

A few years ago I was leaving my parents driveway in the morning, I get into my car, started it up and started turning the steering wheel when suddenly I heard a crack noise and lost the ability to turn the wheels.

As my knowledge of car mechanics it’s almost inexistent and one of my father friends is a mechanic, I called him to take a look into it and gone through with my day, knowing I’ll retrieve the car at the mechanic’s garage later.

At the end of the evening, I went to the mechanic’s, paid him a beer and the part he changed and got into my car to go to the movies with my new girlfriend.

About 5 minutes driving I heard the crack again and guess what happened, I was without a car for the second time that day. But this time, I had a schedule, I was late for my date and in the middle of the street without my beloved transportation… F*ck.

I literally ran to the mechanic’s garage again to catch him still there and told him what happened.

Long story short, the fix didn’t work at first out and the car  has toed there again. I was left with only one solution, a 1 and a half hour of many stops and subway changes trip to the movies, and yes, do the same again to get back home.

So, what this story got to do with WordPress?

I’ve done something with my car that I’m totally against in my line of work, opted for the cheapest and quickest fix for my problem, instead of making sure it has “the best” solution and it has going to properly solve my problem.

When you cut corners, either to save a few bucks or to have a faster turnaround you’re risking your website future.

When you’re building a website, fixing something in it or expanding its current functionalities you always have a ton of options to choose from. Free or premium plugins and themes, custom development, your cousin friend that understands a little about computers, freelancers, and companies.

Whatever is your choice you need to be sure, what it means for your website future and for its continued functionality and availability.

And what should I choose?

The answer to this question will always be “depends”.

It depends on :

  1. your budget,
  2. the needed functionality
  3. the turnaround you need for it.

My advice would be for you to seek someone that understands:

  1. your needs,
  2. your website and business needs,
  3. the platform where you want to build in, WordPress.

A good professional will be able to tell you what you should do and what would be the best option for your situation.

It’s not always installing the plugin X or Y and Z, it can be a custom built plugin or/and theme.

WordPress is more than a simple CMS it’s a framework, totally customizable for your needs.

You can start small and improve along the way or start with all the bells and whistles. You can even build it yourself, but remember your choices today will impact the way your website works and it’s future.

When WordPress caching system makes things slower

Last week has launch time for my/Samsys latest and last plugin, an internal messaging system for WordPress websites, developed to work together a custom WordPress product for Pre-schools.

The plugin allows registered users to exchange messages between them using a front-end interface ( non-admin users) and a back-end interface on the wp-admin (users with administrative access).

Everything was going smoothly until I started testing on a live website with more than 600 registered users. The front-end ran smoothly but the back-end became painfully slow with over 15 seconds of load time.

After some investigation, I was able to pinpoint the “problem”, multiple databases calls for “update_meta_cache” each call only took milliseconds to process but there were thousands of them and it was slowing down the back-end performance.

What’s “update_meta_cache” ?

As it’s name suggests, this functions updates metadata cache on a specified object and runs  everytime you fetch metada from the database.

“update_meta_cache” is very handy on the front-end where exists a caching mechanism in place but in the back-end, no cache is active and it slowed down the plugin performance painfully. The solution has relatively simple, instead of fetching the users every time I needed them listed I created an object with all the needed user data, saved it using Transients API, and every time a new user it’s added to the website the object it’s re-created with the new data.

 

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 Non 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"
 },