Prorated Product Pricing

Forum Forums Tutorials Prorated Product Pricing

This topic contains 0 replies, has 1 voice, and was last updated by  Anonymous 14 years, 1 month ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #48720

    Anonymous
    Participant

    There may come a time when you want to provide a product with a prorated price based on a certain time period, such as the number of days left in the year. By combining a little PHP code in a custom template with ShopSite’s Variable Priced product option you can easily and automatically create a prorated price for a product.

    In our tutorial, the PHP code will calculate a rate based on the number of days left in the year. The template will then add a hidden field to the form so the price cannot be edited by the user, and it will also display the price for them to see

    Step 1: Custom Product Template

    To display a prorated price your product will need its own template, so to start you will want to create a custom product template and modify the “[-- DEFINE MORE_INFO_PAGE --]” section. In the More Info template, find the section of code that displays the price and replace this with the code below.

    Note: Change the “$dailyrate” variable and the “31 December” date if you want to use a different rate or date in your calculation.

    <!-- ProRated Price Calculation & Display -->
    <?php
    $dailyrate = 1.00; //Per-Day Price in Dollars
    $endofyear = strtotime('31 December')
    $now = time()
    $timeleft = $endofyear - $now
    $daysleft = round((($timeleft/24)/60)/60)+1
    $prorated = $dailyrate * $daysleft
    $prorated = number_format($prorated, 2, '.', '')
    ?>
    <!-- Hidden Field for Pro-Rated Price -->
    <input type="hidden" name="[-- PRODUCT.RecordNumber --]:price" value="<?php echo $prorated; ?>">
    <!-- Display of Price -->
    <b>[--STORE.Price --]:</b> [-- STORE.CurrencySymbol --]<?php echo $prorated; ?>
    <!-- End of ProRated Price Display -->
    

    What This Does: This PHP code will calculate a price based off the date, will then pre-fill a hidden price field, and will display the price to the customer.

    Step 2: Product Configuration

    Since PHP code is used you need to give your product’s More Info page a filename ending in .php (ex: myproduct.php) so the code can be executed. Another option is to modify your .htaccess file to process PHP code in all .htm/.html files. The Variable Price option will also need to be turned on, so you will want to edit the product that you want to give a prorated price to and set the following options:

    Note: The Price field for your product can be $0, or when set to any other amount it will act as a minimum value. For example, if set to $25 and the prorated price calcualted to $19, when added to the cart the price will be automatically increased to $25.

    Price: 0.00 [Or higher to set a minimum price.]
    Variable Price: “Check here to enable variable pricing” – Turn On
    More Info Page File Name: filename.php (Name approriately for your product and end the filename in “.php”)

    Also, edit your product’s layout and set it’s Template field to the template you created in Step 1.

    Note: This change and example covers the calculation and display of a prorated price on a product’s More Info page. If you want to display a prorated price on a category page that lists multiple products, you would also need to modify the “[-- DEFINE PRODUCT --]” section of your product template and your category page would need to be able to execute PHP code.

    To see a working sample of a product with a prorated price, please see our demo store here:
    http://shopsite-demo.lexiconn.com/prorated.php

    Advanced Options
    Instead of hard-coding your price and date into your PHP code, if you need to create prorated prices for different rates or date ranges, ShopSite’s Extra Product Fields could be used. Under Preferences > Extra Fields you can name the extra fields and then configure their values on the Edit Product Info page.

    For example, let’s say you decide to name “Product Field 2″ to be “Prorated Date” and “Product Field 3″ to be “Prorated Price”. When you edit your product, set the “Prorated Date:” price field to your date, and it must be in a “D Month” format, such as “31 December” or “4 March”. Then set your “Prorated Price:” field to the daily rate. This has to be only a number (no $), and decimals are optional – such as “2″ or “1.95″.

    Then change the following lines of the PHP code in your template:

    $dailyrate = 1.00; //Per-Day Price in Dollars
    $endofyear = strtotime('31 December')
    

    to be:

    $dailyrate = [-- PRODUCT.Field3 --]; //Per-Day Price in Dollars
    $endofyear = strtotime('[-- PRODUCT.Field2 --]')
    

    Now when you publish your site, the PHP code will use the values of PRODUCT.Field3 (Prorated Price) and PRODUCT.Field2 (Prorated Date) instead of the hard-coded values.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.