Woocommerce: Add single product notice on a per category basis with hierarchy

Recently had a client come to me on Codeable who wanted to display a notice below the product description based on category. The client wanted to choose which categories this would show on and wanted the notice to show on each child unless it was overridden on the child.

I utilized Advanced Custom Fields which is a great plugin to give us a toggle switch and a paragraph box. You could code these yourself as taxonomy meta boxes but why do that when you have access to such a great plugin to do it for you! Also a side note, you do not need the premium version of ACF for this but it has other great features so please check out their website!

First what you want to do is create the fields in ACF, I created a toggle true/false field for the switch and a WYSIWYG editor for the disclaimer text. Once I set up my fields I set the fields to only show up on the Woocommerce taxonomy product_cat.

Once the fields were created we can go in and set up the category

 

Once the disclaimer is in then it’s time to add our custom code! You want to add this in your child theme if you have one in the functions.php file. If you do not have a child theme there are a ton of tutorials out there to create one! The code below relies on the field names, if you have different field names then update your code accordingly!

 

With a little styling here is the final result!

 

WordPress Error: Site Downloads PHP Files Moving to another host from BlueHost

Recently I had a client  on Dreamhost come to me with a problem where the wordpress files would download to their computer. I know this problem well, usually it’s a php handling error, basically php is not parsing the file and the file is being served as a download. The first place to look for something like this is the .htaccess (the client was running apache). I found the following code in the htaccess

AddHandler application/x-httpd-php70 .php

So removing this piece of code brought the site back up, but how did it get there?

After a bit of research and digging around I found that there was a plugin created by bluehost called Endurance PHP, I asked my client if the site in question was previously on bluehost and they confirmed. So the client built the site on Bluehost then transferred it to Dreamhost and a trigger of the permalink rewrite added the “AddHandler” code into the htaccess.

So to fix the issue all I had to do was remove the 3 files from

/wp-content/mu-plugins/

The files in question area

endurance-php-edge.php
endurance-browser-cache.php
endurance-page-cache.php

After removing the files you need to also delete .htaccess file and then go settings and save permalinks (generating a new .htaccess file)

This is why I always recommend a reputable host like Liquidweb!


Optima Express Yoast SEO integration

Recently a client came to me with a problem with a popular WordPress plugin called Optima Express by ihomefinder which looks like a great IDX plugin for real estate companies. The problem was that it did not support Yoast SEO. This is a huge problem considering Yoast is one of the biggest SEO plugins out there and it was basically passing no title tags to properties.

After inspecting the code I noticed Optima was forcing it’s own data onto a page post type and passing it as ID 0. This results in problems for Yoast or anything else using the post object because basically it was trying to find a title for an ID that did not exist.

To fix this we need to repass the title from the post object to the filters supplied by Yoast SEO. Here is the code, you can place this in your functions file or make a plugin for it.

WordPress Document Manager – How to send an email after download

Recently someone came to me with a problem, they needed to fire off an email from the popular wordpress plugin Smarty Pants Client Document Manager. The plugin did not have that default functionality but it does supply the hooks to do so.

To attack this issue we needed to find two hooks

“sp_download_file_after_query” Which fires after a user has downloaded a file

“sp_cdm_settings_email” which adds a system email to the settings page

In the email settings function we are just creating a simple email that is integrated into the smarty email editor.

In the function we created for the download you can see how we took the file array and from that array got information about the file owner, project owner and current user. With that information we can take the content from the email we created and do anything we want with it. In this instance I’m taking the file owner and sending them a notification that someone has downloaded a file.

Since we built this into the email manager you have full use of the email shortcodes, so that makes this method versatile to do pretty much anything from emailing to submitting information to 3rd party APIs. Check out the plugin here and the code below.

Retry WooCommerce Subscriptions gateway failed renewals

Recently I had a client come to me who had a gateway error, woocommerce subscriptions was not processing renewals correctly. So after a few days we fixed it but there was about 600 failed renewals for $5 each that needed to be re-entered. Since woocommerce doesn’t have a way to to do this I needed to create a script that would loop through failed and pending orders and try the renewal again. If you use this script just create a plugin file and navigate to ?retry=1  or use the funciton in your own file!

https://gist.github.com/anthonysbrown/f87e220429c13c6913a46ed59f9d158d