Sorting the country select in Shopp

This came up recently while working on a site that had a bunch of target markets selected in Shopp. The country dropdown list on the Checkout page was in a somewhat random order, and I realized that it was displaying them in the order they were selected or saved. When I checked 3 new countries, they showed up at the bottom of the list. So, knowing that, we decided it would be better for everyone if we sorted the list alphabetically. The code is below:

/* Sort Countries in dropdown alphabetically */
add_filter( 'shopp_countries', 'my_country_sort' );
function my_country_sort( $_ ) {

	$countries = array_msort($_, array( 'name' => SORT_ASC ) );
	// Åland Islands (EUR) sits at the bottom, so move it to the top
	$countries = array('AX' => $countries['AX']) + $countries;
	return $countries;

}

You’ll note the comment about Åland Islands; ideally we want it to be listed with the other A countries, but I don’t know if there’s a way to sort special characters as their originating character in PHP. So, we pop it off the end of the array and push it back to the top.

Shopp Minimum Order Amounts 1.3 Released

I just released a new version of my Minimum Order Amounts plugin for Shopp today. This new version finally supports version 1.3 of Shopp, and allows merchants to set a minimum quantity for each product in the catalog. The product minimums are separate from the storewide minimums, and both are checked before the customer checks out.

You can find it in the WordPress Plugin Directory!

Disabling Multiple Clicks to the Submit Order button in Shopp

Most of us who use web browsers understand that the web UI only requires a single click on links, form buttons, and input fields. However, there are people who are used to double-clicking everything in the same way you double-click icons in an OS GUI. Sometimes this causes problems, specifically with shopping carts, where double-clicking can lead to the checkout form being submitted twice which results in duplicate orders.

I ran into this problem with Shopp, in a very specific situation where the server was responding slowly. Normally this kind of thing doesn’t happen, and I couldn’t reproduce the problem myself, but a few customers over a couple of days had managed to place duplicate orders. After checking the database I noticed that the transactions were usually 2-15 seconds apart, so I thought I’d disable the “Submit Order” button on the checkout form after it had been clicked once. Easier said than done.

Unfortunately, this isn’t as simple as using the disabled attribute on the button. Since there is some Javascript input validation, I needed to make sure that the form was validating first before disabling the button. Fortunately, I had spoken to the lead developer of Shopp about this a while back, so I knew that I needed to check `shopp.validate` which fires when the Submit Order button is clicked. If it validates correctly, then it submits the form data to the server. After some digging in the JS, I found the validation method and wrote some code to disable it after the button has been clicked once. Here’s the code:

<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function($){
	var form = $("#checkout.shopp"),
		checkoutButton = $('.payoption-0 input');

	form.on("submit.validate", function(e) {
		if ( validate(this) ) {
			form.unbind('shopp_validate');
			checkoutButton.unbind("submit.validate").attr("disabled", "disabled").attr("value", "Processing...");
			// set a timer to re-enable the button or maybe reload the page
			setTimeout(timeouterr, 30000);
		}
	});

	function timeouterr() {
		$('.payoption-0').append('An error has occurred, and your order has not been sent in 30 seconds. Please <a href="javascript:location.reload();">reload the page</a> and try your transaction again.');
	}
});
// ]]></script>

All you have to do is drop this code into the bottom of the checkout.php file in your theme/shopp directory.

The code runs when the “Submit Order” button is clicked, and it checks to make sure the form is valid. If it is, it disables the validate function, which posts the data to the server, and then it changes the button text to “Processing…” so the customer knows something is happening. In the event that something doesn’t happen after 30 seconds, the `setTimeout()` function will display a message below the button that says the order hasn’t gone through, and shows a link to reload the page.

This code has stopped the duplicate transaction issue I was seeing, and it offers a better user experience especially on slow sites where a customer might click on the submit button because it doesn’t look like anything is happening.

WP-Property is great

This week I’ve been working on a new website for Lava Rock Realty to promote their property management services by showing their vacation rentals on their site. My first instinct was to use custom post types to create something custom that would do exactly what we needed. I figured it wouldn’t take very long and would be better than evaluating a bunch of 3rd party plugins to find one that did everything my clients needed.

Fortunately, as I was writing the code to setup the custom post type, I thought I’d do a quick Google search to see what was out there for showing listings and properties. Other WP devs must have written something, right?

One of the first plugins I found was WP-Property, and it looked like it would do everything I needed. After installing it and spending a few minutes playing with it and familiarizing myself with how it works, I knew I didn’t need to look any further. This plugin is great for showing listings, and has some premium addons that may be useful in other projects (I’m looking at you MLS/RETS/XML importer).

That said, there are some tweaks that need to be made to the base templates, which I’ll cover in an article later. Otherwise, this plugin is great!

Changing the default WordPress email sender

It’s not uncommon to want to change the email sender that WordPress sets automatically, especially in e-commerce situations. It’s actually really easy to do by adding a couple of filters in your theme’s functions.php file:

/** change the default WordPress email sender */
add_filter('wp_mail_from', 'my_mail_from');
add_filter('wp_mail_from_name', 'my_mail_from_name');

function my_mail_from($email) {
return 'customerservice@example.com';
}
function my_mail_from_name($name) {
return 'Customer Service';
}

Filtering excerpts in WordPress

By default WordPress displays […] at the end of an excerpt, which doesn’t look good. Instead, I use this functions on blogs that I build, which changes it to something that looks better. Just add the following to your active theme’s functions.php:

function new_excerpt_more( $more ) {
return '... <a class="moretag" href="'. get_permalink($post-&gt;ID) . '"> Read More »</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');