WordPress e-Commerce and Product 404’s, solved!

I’m building a website for Wildly Natural One and it’s going to include, among other things, an online store. While I initially leaned towards a Zen Cart installation, the various client desires (most notably a way to purchase without creating an account) pointed me towards the wp e-Commerce plugin for WordPress itself.

Overall, I’ve been pretty happy with it so far – having the admin tool built directly into the WordPress admin (and not having to skin Zen Cart as well as WordPress) has been nice. I did manage to run into a non-trivial issue – at some point all my single product links broke. Instead of the product page, WordPress would serve up a 404. Ugly. I searched all over the internet and found all kids of people with the same problem, but none of their solutions worked. I decided to delve into the code and here’s what I found:

When wp e-Commerce injects its permalinks into WordPress, a function named wpsc_product_permalinks in wpsc-includes/display.functions.php is called. This function looks for all the posts in the WordPress database that has [productspage] anywhere in the content that isn’t a revision. It then takes the first result from that query and builds the permalink from there.

The problem is (and I think this is new to WordPress 3.0 and the neat-o Menu admin tool) that there isn’t always a single post that has the content [productspage] in it. What I found in my database is that there were a few posts of type nav_menu_item with that content in there along with the expected page result. The query was returning one of the nav_menu_item posts rather than the page, and all my individual product page links were broken.

To fix this, I changed the query inside the plugin file from:

$page_details = $wpdb-&gt;get_row("SELECT * FROM `".$wpdb-&gt;posts."` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` <strong>NOT IN('revision')</strong> LIMIT 1", ARRAY_A);

to

$page_details = $wpdb-&gt;get_row("SELECT * FROM `".$wpdb-&gt;posts."` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` <strong>NOT IN('revision', 'nav_menu_item')</strong> LIMIT 1", ARRAY_A);

Note the bold part. Pretty easy fix, although it wasn’t that easy to find. I’ll be submitting this to the wp e-Commerce team, but in the meantime if you’re getting 404’s on your product pages give this fix a try!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>