Spruce interactive


Free Downloadable Products with WP e-Commerce (WPEC)

Posted: June 20th, 2012

Author: Ben Heller


, , , , ,

 Details

Many of our clients use WP e-Commerce to host free downloadable products, usually mixed in with priced items through multiple payment gateways. Unfortunately, WP e-Commerce doesn’t support this, and their forums suggest all manner of wacky schemes to trick the platform into skipping checkout. In our case, we usually want to capture the customer’s information, but route them directly to the transaction results page with a status of “payment accepted” so downloadable files are presented immediately, rather than marked as pending approval. And, we want to do this without forcing them to select a payment gateway in the first place, because that just doesn’t make sense if they won’t be paying for the download. Our main criteria here is that we don’t have to edit any of the core plugin files, but can make the edits using exclusively those files that WPEC copies to a user’s theme directory, in combination with our default functions.php. So, here we go!

Hide the Gateways if the Cart Total = $0.00

Open the wpsc-shopping-cart_page.php file in your theme directory, and find the line that says “//this HTML displays activated payment gateways”. Modify the first
tag beneath that to read:

<tr <?php if ($wpsc_cart->calculate_total_price() == '0') {echo 'style="display:none"';}?>>

Mark the Transaction as Payment Accepted

This is a little less intuitive than the last step. Here we’re using a WordPress action to add a function that modifies the wpsc_submit_checkout function in WPEC. First we call the requisite variables, then set the purchase log location using a SQL query so we can update it correctly. Next, we determine whether the purchase log registers a zero dollar transaction for this sale, and if so we generate a new merchant gateway to send the purchase through that then sets the status to “accepted payment”. Just add the code below to your theme’s functions.php file.

// SKIP PURCHASE and Mark as Accepted 
add_action ('wpsc_submit_checkout', 'freepurchase'); 
function freepurchase ($purchaseinfo) {   
  global $wpdb, $wpsc_cart, $wpsc_shipping_modules;   
  $purchase_log_id = $purchaseinfo['purchase_log_id'];   
  $purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id`= ".$purchase_log_id." LIMIT 1",ARRAY_A) ;    
  // Fix for $0.00 sales   
  if ($purchase_log['totalprice'] == '0.00') {     
    $sessionid = $purchase_log['sessionid'];     
    $free = new wpsc_merchant_testmode( $purchase_log_id );     

The beauty of this solution is that we can continue updating WP e-Commerce without fear that our edits will be overwritten every time we do so. Of course, it’s always worth checking to make sure this fix remains compatible with the latest version, but that’s better than having to re-implement it each time.

  • CalLabSolutions

    I am having the same problems on my WordPress Site.. And I am looking for someone who know how to fix the problem..

    • Spruce Interactive

      We’d be happy to help out with any WPEC modifications you might need! Drop us a line using the contact form below and we’ll get the ball rolling.

  • Dani

    This did not work for me. Not sure why but will try a few things.

    • spruceit

      Hi Dani, it might be that you’re running a different version of WPEC than we are, or it could be a conflict with other plugins or edits you’ve made to the theme. We’d be happy to help you sort it out–feel free to drop us a line via our contact form. Thanks!

      • Dani

        Thanks for the help again! Turns out when I copied and pasted it all the formatting disappeared making it one giant lump of text. Once we got the lines arranged properly like they are here it works perfectly!

  • Sev

    Thank you very much !! It works :)

  • MayeCreate

    You are so super uber cool for posting this! Thank you so much! It worked perfectly and on the first try!!!

    • spruceit

      Yay! Glad to hear it :-)

  • Davec

    Can you be a bit more specific as to where this is inserted. I have tried a couple different ways and its not working. Thanks for posting this!

    Hide the Gateways if the Cart Total = $0.00

    <tr calculate_total_price() == ’0′) {echo ‘style=”display:none”‘;}?>>

    • spruceit

      Hi Dave,

      In the most recent version of WPEC ( this is in wpsc-shopping_cart_page.php at line 416. Replace the table row there with the code snippit we posted above and you’ll be good to go!

  • saadane

    It work like a charm
    Thanks for sharing and for your time
    How can i buy you a beer ?
    what’s about a donate button

    • spruceit

      You’re too kind! No need to buy us a digital beer–just spread the word about our solution, and keep us in mind if you need some help on a project :-)

  • Rudra

    Thanx a tone
    You really saved my day.
    Was looking out to fix the stuff from past 3 days
    Finally i have got reference which works as a charm

    • spruceit

      No problem, glad to help!

  • ezk12lessons

    Thank you so much for this fix! It worked perfectly for me. Now, to test it with my customers.

    • ezk12lessons

      Actually, I just tried to include a priced item with my free items and it takes me to the Paypal page and does not give me the free downloadable items until I am sent back to the Transaction Results page. Is there a way to easily open the Paypal window separately? I am afraid my customers may end up closing out the window before being automatically taken back to the Transaction Results page.

      • spruceit

        Hi there–unfortunately, the transaction results page needs to show the entire contents of the cart once purchased, so customers ordering both free and non-free items will need to pay for the non-free items before viewing the transaction page. You could technically split the order into two purchases, show one transaction page for the free items first, and then take users to a new transaction page for the paid items, but this would be a VERY complicated fix and would require extensive edits to the WPEC core. You might consider writing a plugin for this functionality, but it might be a fairly limited use case. In most instances, customers will understand that they need to paid for their items before viewing the transaction results page with all their downloads (both free and non-free).

  • Guillaume Cribier

    Thx a lot for you snippet, for other don’t forget to edit the good file if your another theme that default .

    I forgot for a few moments ;-)

  • http://www.facebook.com/patricktang Patrick Tang

    Thanks for the useful snippet! :)

  • Gavin

    Lovely idea, unfortunately not working for me on WP e-C Any idea what line this should be on for this version please?

    <tr calculate_total_price() == ’0′) {echo ‘style=”display:none”‘;}?>>

    Any advice much appreciated! Thanks

    • spruceit

      It looks like on, you’ll need to add this code on two lines: 448 and 468, on each of the tr tags. Give that a go!

      • Gavin

        Thanks for your response. I couldn’t get it to work unfortunately, just takes me through to a Transaction Results page with an order total of £1.00. Will upgrade the latest version and wait to see if a solution is posted here for Thanks again.

        • Ricky Hewitt

          I’m running and the two lines mentioned by spruceit (448, 468) seemed to do the trick!

  • Sam

    How do you do that on

    • Aiman

      I’m also looking for this on =(

      • Sam

        I’m able to do it on, same setup as above.

        • Aiman

          AWESOME! Appreciate it! Tried as per instructions, worked like a gem!!

  • http://twitter.com/freshlinx Freshlinx Web Design

    Fantastic! Thanks for sharing! Been wondering how to do that! Genius!

  • rwbronco

    seems to be working on the latest WPEC as of April 2013. I’m using a theme from Splashing Pixels and it’s working just fine for me

    • http://www.facebook.com/DeogharOnline Himanshu Singh

      it is not working for me

  • dave

    I can’t get it to work on Version ….any suggestions!

Talk to Us

Wise men have said, "All good things must come to an end", and so it was with Spruce. We began our first project in 2006, and completed our last in Fall 2013. It's been a wonderful experience, but now it's time to begin the next adventure.

Though...we wouldn't want to leave you hanging, now would we? Here are our top recommendations for world-class branding, design, and web development:

  • Kristina (KJ) Parish and Beam Collective:
    KJ provided the artistic vision that allowed Spruce to succeed, and we still think she's the greatest designer in the entire world. She's started a new collective with some talented developers, and is covering much the same territory as Spruce--design and development all under one roof, with a caliber of service and professionalism seldom seen in this business or any other. If you like what you see on the Spruce site, then definitely get in touch with KJ at kj@beamcollective.com
  • Permanent Art & Design
    Permanent is a nationally recognized design & branding firm out of Minneapolis, MN. They've been a long term collaborator and strategic partner over the past couple years, and we'd recommend them for medium to large sized businesses/organizations who are serious about putting their best foot forward. In addition to design, Permanent offers strategy and marketing services, plus in-house and networked developers. For more information contact Joseph Belk joseph@permanentadg.com
  • Nate Thompson
    A freelancer, Nate reminds us of ourselves back in the day. Smart, communicative, and skilled, Nate is our recommendation for small to medium sized businesses/organizations who have sites built on Wordpress or other open source content management solutions. With a background in both design and development, he's a one-stop shop for most web-related tasks. You can get in touch at nate.a.thompson@gmail.com
  • Kevin DeBernardi
    Designer turned developer, Kevin is embarking on a freelance web career after working as the in-house designer at the Museum of the City of New York. He's adept at translating his design ideas into custom PHP code, and is constantly expanding his technical palette. Kevin's a good choice for projects that don't quite fit into the Wordpress mold, and that would benefit from a consistency of presence and vision from design through execution. Kevin can be reached at kevin@analoglifestyle.com