Open ‘Calculate Shipping’ Box on WooCommerce Cart Page Load

I’ve seen this request a couple times while helping out WooCommerce customers so I thought I’d share a quick CSS snippet that will do just that.

Add the following to your custom.css file or with a plugin like Simple Custom CSS:

.shipping-calculator-form {
    display: block !important;
}

This will make the Calculate Shipping box automatically open when the Cart page is loaded.

Add PDF to WooCommerce Order Email

Quick little snippet that will let you add a static PDF file (or in fact, any kind of file) to the WooCommerce Order Emails.

Just add the following code to the ‘custom functions’ area of your functions.php file:

Delete the php first line if you are having errors come up after saving the file.

Now simply edit the $your_pdf_path variable to be wherever your PDF (or other file) is located!

Too easy, right?

Reference: http://wordpress.org/support/topic/pdf-attach-terms-and-conditions-to-email

Order Products by ‘On Sale’ in WooCommerce

Have you ever wanted to place your on sale items at the top of your product listings? You can pretty easily do it in a couple of ways.

Screen Shot 2014-07-14 at 3.51.59 pm

If you would like to add a ‘On Sale’ option to the ‘orderby’ menu, so users can choose to order it themselves, add the following code to the ‘custom functions’ area of your functions.php file:

However, if you would like to make it the default order, add this:

I hope that helps you out!

Change the WooCommerce Auto Generated Password

In WooCommerce, there’s a setting under WooCommerce > Settings > Accounts to automatically generate a customer’s password. This way user’s only have to enter an email when they register (with the password generated and sent to their email address), similar to how WordPress handles registration by default.

WooCommerce uses WordPress’ core wp_generate_password() function to generate the password, but you may want to change this.

You can do this by filtering woocommerce_new_customer_data, declared around line 102 of woocommerce/includes/wc-customer-functions.php:

So, if for example you wanted to make the password generate as the user’s email, you could use the following:

Or perhaps you want to use the wp_generate_password() function but customise it a little bit, so that it’s only 6 characters and uses all special characters. Something like this would work:

If it helps you out or you were able to customise and make something even more awesome, let me know!

Check if Another WordPress Plugin is Active

Recently I’ve been spending a lot of time working on my new plugin Recipe Hero. Essentially, the plugin is an open-source, free item, but the extensions that I (and others) make for it, can be either free or premium.

Similar to WooCommerce, Easy Digital Downloads, WP Job Manager and even WordPress itself, the core item itself is a simple, sufficient plugin, but the functionality to take it that little bit further and do something ‘out of the norm’, should be in its own separate ‘extension’ or ‘add-on’.

However, what if a user doesn’t realise that and tries to add an extension without the ‘core’ plugin? Or how about when a user deactivates the core plugin but forgets about deactivating the extensions. We need to put a check in for that or you’re going to be dealing with some angry users.

WooCommerce actually has a great article on Creating a plugin for WooCommerce, that gives it a great example of how to check for another plugin. A lot of people like to use is_plugin_active, but I like WooCommerce’s method better, as is_plugin_active is a little bit less flexible. Themergency also has a post looking at different methods to check if a plugin is active.

The approach WooCommerce takes is to check if the woocommerce/woocommerce.php file is in the array of active plugins, using PHP’s in_array function.

So if we wanted to check if WooCommerce was active, they suggest to use the following:

In a Recipe Hero extension, Recipe Hero Likes, I need to check if Recipe Hero is active before requiring a couple files that contain most of the extension’s functions. The following is what I use:

How do you like to check if a plugin is active? Do you think there’s an issue using in_array? Let me know in the comments!

PHP: Convert 24 Hour Time to 12 Hour Time (and Vice Versa)

I just came across this in a time of need and thought I would share.

If you want to convert a time in 24 hour format, such as 14:00, to 12 hour format, which would be 2PM, you can use the following code.

24 Hour to 12 Hour:

Additionally, if you wanted to convert the 12 hour time format to 24 hour, you can use the next bit of code.

12 Hour to 24 Hour:

PS. Sorry I haven’t posted in a long long time. Been travelling, living, etc. Coding again and working on some new stuff so will try to post more often and reply to questions. Cheers readers!

Change ‘Add To Cart’ Text in WooCommerce

UPDATE: New updated post here!

Whenever there’s something I struggle to find a clear answer for (and eventually figure out myself), I like to post it for those who need help later. This is one of those times.

If you ever wanted to change the ‘Add To Cart’ text on your WooCommerce site, just add in the following code to your functions.php file (or similar):

Don’t forget to change the text to whatever you’d like your button to say!