Filtering Free Shipping Discounts in Shopp

When most people think of Hawaii, they picture palm trees, sandy beaches, mai tais, and sunsets, or sitting under a palm tree, drinking mai tais on the beach while watching the sunset. I have to admit that living in Hawaii is pretty great, but one of the things that most people don’t think about is that there is no such thing as cheap shipping to Hawaii.

We’ve all see Free Shipping promotions online, and if you’re lucky enough to live in the 48 contiguous states you probably don’t even think twice about it. But if you read the fine print, you’ll find that Hawaii and Alaska are almost always excluded. Even Amazon Prime, with it’s “Free Two Day Shipping” is actually “Free Standard Shipping (4-5 Business Days)” for Hawaii. Not that I’m complaining; I’ll take that over freezing winters and tornadoes any day.

But I digress. Let’s get to the code.

If you happen to be running a Shopp store, and you’re offering a Free Shipping for orders over $X promotion, you’ll likely want to make sure it doesn’t apply to customers in Alaska and Hawaii. The Discounts editor doesn’t currently support this, and the best you can do is set it up to only include US orders. See the setup below:

Free Shipping Discount Settings

Now, in order to exclude AK and HI, we’ll need to use an undocumented filter in Shopp called shopp_apply_discount, which runs once per active discount when the cart and order totals are being calculated. The filter passes two values: $apply which is a boolean for whether or not to apply the discount to the order, and $Promo which stores the contents of the ShoppPromotion object (which includes the type of discount, discount rules, etc). It expects a return value of true if the discount should be applied, or false if not.

Next we need to know the ID of the free shipping discount, so we know which one to exclude. The easiest way to find this out is to look at the link on the Discount editor page. For the sake of simplicity, we’ll say it’s 1.

There are two ways to go about excluding the discount from the cart. Either we could allow it to apply to the cart by default, then remove the discount after the customer has chosen their shipping state (or entered their zip code in the Estimated Shipping portion of the Cart). Otherwise, we could choose to only apply the discount after the customer has chosen their shipping state. For this example I’m leaving the discount disabled until after the customer chooses their shipping state, but you’ll have to decide which method works best for your site.

Drop the following code into your theme’s functions.php file:

// Filter the discounts and do not apply FREE SHIPPING until the shipping state is chosen
add_filter('shopp_apply_discount', 'no_free_shipping_for_you', 10, 2);
function no_free_shipping_for_you ($apply, $Promo) {

// set the ID of the Free Shipping promo
$shipping_promo = 1;
if ($Promo->id == $shipping_promo) {
    $Shipping = ShoppOrder()->Shipping;
    $Billing = ShoppOrder()->Billing;

    // Use the shipping state, with the billing state as a fallback
    $state = isset( $Shipping->state ) ? $Shipping->state : $Billing->state;
    shopp_debug("Free Shipping Promo match: State is '$state'");
    if ( empty($state) || $state == "AK" || $state == "HI" ) return false;
}

return $apply;
}

That’ll do it. If you wanted to have the discount apply all the time, except after a customer has chosen AK or HI, then you’d change the last if statement to this:

    if ( $state == "AK" || $state == "HI" ) return false;

One thing to consider is that if a customer doesn’t have an account on your site, you probably won’t know their shipping state until the Checkout page, except if the customer enters their post code in the Estimate Shipping & Taxes field in the Cart. Since this code may change their order total (either by removing or adding the free shipping discount during Checkout) they’ll see the difference on the Confirm Order page. It’s not ideal, but it’s the best we can do in this case.

I should note, this code will only work in Shopp 1.3.x. I’m not sure the filter existed before that, and the ShoppOrder() function definitely isn’t in 1.2.x.

Upgrading Shopp 1.1.9 to 1.2.3

Shopp 1.2 has been with us for some time now, but prior to 1.2.2 there were problems with upgrading from 1.1.x. Now though, upgrading to 1.2.3 (the current version as of August 2012) from 1.1.9 is incredibly simple. Before proceeding, I recommend reading the documentation for upgrading to Shopp 1.2 and the template changes in 1.2.

The best way to proceed with an upgrade like this is to create a new sub-domain on your server where you can try the upgrade without making changes to your live site. Copy your site files to the new sub-domain, and copy the WordPress database to a new database. Once that’s done, you’ll need to update wp_config.php on the new domain to point to the new database, and also update the ‘siteurl’ line in wp_options to point to the new sub-domain. Once that’s done, you can proceed.

Backup Your Database

This is essential before doing any upgrade in WordPress, but many of us omit this step. I’ll admit, I’m guilty of this from time to time as well. We sort of expect that when we upgrade, everything will work fine, and backing up can be a pain. There are a number of plugins that will back up your database for you, but I prefer to go into phpMyAdmin to export the whole database. Once that’s done, we can move on.

Backup Your Files

You shouldn’t need to backup the entire site, as we’ll only be upgrading the Shopp folder, so you can get along just fine by downloading a copy of your wp-content folder. If you haven’t got a backup copy of your entire site in some time, this is a good time to do so.

Note Your Settings and Addons

When you upgrade Shopp, most of your settings are carried over properly. However, in the event that they’re not, it’s a good idea to have a copy of your settings on paper just in case. Go through the Settings page and make notes of your settings and set that aside. Also, if you use shipping or payment addons, make notes of your settings there as well. I discovered that when I upgraded to 1.2.3, Authorize.net forgot which credit cards it had been set to accept.

Get Fresh Downloads

Go to your Shopp account page and download the latest copy of Shopp core, and any addons that you’ll be using on the site.

Now, you’re ready to proceed with the upgrade. Cross your fingers, and here we go.

Maintenance Mode

If you *have* to upgrade on your live site (I mentioned that this is a bad idea, right?) then find a maintenance mode plugin and enable it before proceeding. This way, visitors to your site won’t get unfriendly error messages while upgrading.

Update Shopp Folder

Disable the Shopp plugin from within WordPress. I like to preserve the old Shopp folder, so move it to the parent folder (wp-content), then upload the new Shopp files. Once it’s done, upload the shipping any payment addons to the appropriate folders. When that’s all done, enable the Shopp plugin again.

Update Menus

One of the biggest changes in 1.2 is that Shopp no longer requires Pages to work. It uses “virtual” pages which are loaded from the new template files, and the page slugs are set from within the Shopp settings. Any menu references to the old Shop, Cart, Account, and Checkout pages will not work, and you’ll find these pages in the Trash. You’ll need to replace them with Custom Links.

Update Theme Template Files

Because Shopp is no longer using Pages, you can no longer choose a Template for those pages, or choose a layout option (available on some theme frameworks). This means you’ll likely need to create some new templates, or update your existing templates. The changes required generally differ depending on which theme or framework you’re using, but you’d be served to update your Theme to the latest version available. I’ll cover more specific changes in another article (to come later).

At this point, you should be done. Check thru your site to make sure there aren’t any major problems.

OMG THINGS ARE BROKEN!

If you’ve arrived at this point, then it’s a good thing you did the upgrade on a test site and not your live site, right? There are a slew of issues I’ve seen people have, and each one is a different animal, but the best thing to do is try narrowing the problem down as much as possible. My advice is to contact the Shopp Help Desk and let them help you out.