Forum

Thread tagged as: Question, Shop

Output lowest variant price in product listing template

Has anyone any idea how I might go about displaying just the lowest variant price for a product in a product listing template?

I'm thinking of having "prices from" in the product listings, followed by the lowest of any variant prices associated with that product. (If there aren't variant prices then the product price is used instead.)

I can't seem to get my head around how to drill down into the array that contains the variant prices.

I'm only at planning stage so have nothing to post other than my diagnostics.

Many thanks in advance for any pointers.

Jon

Perch Runway: 3.0.5, PHP: 7.0.15, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (3.0.5), assets (3.0.5), categories (3.0.5), perch_shop_orders (1.2), perch_shop_products (1.2), perch_shop (1.2), perch_members (1.6.1), perch_mailchimp (3.1)
App runtimes: <?php $apps_list = [ 'perch_mailchimp', 'perch_members', 'perch_shop', ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /Users/ellimondo/Dropbox/Sites/ellipress/dev.ellipress.co.uk/perch
PERCH_CORE: /Users/ellimondo/Dropbox/Sites/ellipress/dev.ellipress.co.uk/perch/core
PERCH_RESFILEPATH: /Users/ellimondo/Dropbox/Sites/ellipress/dev.ellipress.co.uk/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 0c66c2e1f82f9e0b7617b2cb8270f2c7
Resource folder writeable: Yes
HTTP_HOST: dev.ellipress.co.uk
DOCUMENT_ROOT: /Users/ellimondo/Dropbox/Sites/ellipress/dev.ellipress.co.uk
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Jonathan Elliman

Jonathan Elliman 27 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think you'll be able to do that with just a template.

So I've got it working through a callout function (I knew nothing about these yesterday!), but I know it's not 100% right as I am a designer numpty.

As far as I can make out, the callout function gets the lowest variant price for each product and puts it into the array for the product listing. Which is great.

    <?php

        perch_shop_products([
            'template' => 'products/product_list.html',
            'variants' => true,
            'filter' => 'featured',
            'match' => 'eq',
            'value' => 1,
            'count' => 3,
            'each' => function($item) {

                $lowpr = perch_shop_product_variants($item['productSlug'], [
                 'skip-template' => 'true',
                 'sort' => 'price',
                 'sort-order' => 'ASC',
                 'count' => 1,

                ], true);

                if (isset($lowpr[0]['price']))

                    {
                    $item['lowestprice'] = $lowpr[0]['price'];
                    };

                return $item;
            },

        ]);         

    ?>  

And then in the template I am asking if there's a variant price and outputting the lowestprice id into the page

                    <perch:if id="productHasVariants" match="gte" value="1">
                From £<perch:shop id="lowestprice" output="raw" type="hidden" format="$:%.2n"/>
                </perch:if>

The only outstanding issue is that when I assign type="shop_currency_value" to the "lowestprice" id in the template it doesn't output at all. When I assign type="hidden" the value is displayed but I have to hard code in the currency symbol. I will also come a cropper when/if a sale or trade price is chosen but I will deal with that another time!

Any tips on improving things would be greatly appreciated.

Kind regards

Jon