Show the WP e-commerce shopping cart widget only on store pages

I’m using the WP E-Commerce plugin on a site I’m working on, and didn’t like the default cart options. The only options were to hide the cart when empty, or always display it.This might make sense on a primarily e-commerce site, but e-commerce is just a small sliver of the purpose of the site I am working on.

Instead, I wanted to display it a) Always when the user is on a store related page and b) on other pages only if the cart wasn’t empty.

This involved such a simple change I thought I would post it here for others to enjoy.

 

UPDATED to work with WP E-Commerce 3.8’s Shopping Cart Widget

The file to edit is now wp-content/plugins/wp-e-commerce/wpsc-widgets/shopping_cart_widget.php. You should now edit the widget() function at line 33.

Add this right at the top of the function:

if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && wpsc_cart_item_count() < 1){
      return;
 }

It looks like the hideonempty option is gone. The widget() function just does a bunch of echoes that print out the widget HTML/JavaScript. We now just do a quick check to see if we’re in the store, or if we have some items, and return immediately if we don’t.

In your file wp-content/plugins/wp-e-commerce/widgets/shopping_cart_widget.php on line 8 change

if(($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

into

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && ($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

Alternatively, if you only ever want to show it on store pages, change the line to

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 || (($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1)))

And that should do it!

PS. If you’re looking for a WordPress compatible cart, WP E-Commerce does, unfortunately, seem to be the easiest to use. I say unfortunately because it’s not as easy to set up as most WordPress plugins and the support is somewhat lacking. I have found it adequate but am interested in other, better, cart options if someone comes across one.

This entry was posted in Computers and tagged , , , , . Bookmark the permalink.

9 Responses to Show the WP e-commerce shopping cart widget only on store pages

  1. Tim Ball says:

    Works great thanks, just remember to customise the ‘/^\/store\//’ to ‘/^\/products-page\//’ if you use the default installation of wp-ecommerce.

  2. susan says:

    I got an error:
    Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in /home/path-to-wp-ecommerce/wpsc-widgets/shopping_cart_widget.php on line 34

    I just copied the code onto line 34 so it looks like this:

    if(preg_match(‘/^\/shop\//’,$_SERVER[‘REQUEST_URI’]) === 0 && wpsc_cart_item_count() < 1){
    return;
    }

    function widget( $args, $instance ) {

    global $cache_enabled;

    extract( $args );

  3. robert says:

    I got the same problem as susan. Can you give more detail on how to do this. Thanks

    • stuporglue says:

      I’m afraid that without actually looking at the code I’m not sure what to say, and since it works for me I might guess that you’ve got either a different WordPress version or a different Plugin version.

      Typically Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION indicates that you have an extra closing curly brace (}) closing a previous function and then have an IF statement. If you can figure out where that extra curly brace is, you should hopefully be able to remove it and have everything else start working.

      Unfortunately teaching how to chase down PHP syntax bugs is a bit more than I can do in a comment.

  4. Thanks for the great tip. I’ve impleted this and it works for Chrome and Firefox. However, IE and Safari the cart does not show up at all. Even if I refresh the page. Any idea?

    • stuporglue says:

      I’m not sure what the problem would be in IE and Safari — could it be a CSS bug? The changes are server-side code, so the correct content should be getting printed to the HTML source no matter which browser you’re using.

  5. Fandi Akhmad says:

    Dear all,

    I try create widget to shown quantity of product that user buy.
    But i try test it, that widget not show automatically quantity. But, must be refresh the page to update quantity.
    This my widget code:

    Please help me,

    Thanks

    • stuporglue says:

      Hi Fandi, sorry for the long wait. Unfortunately due to constant WordPress and plugin upgrades, I’ve given up on maintaining these changes. Try upgrading WP and the plugin to the latest version.

Leave a Reply

Your email address will not be published. Required fields are marked *