Forum

Thread tagged as: Question, Shop

Product Comparison

Hi All has anyone attempted a product compassion function where you could click add to compare list on multiple products and then view the details on another page

any thoughts on how to approach this

Thanks

Ant

Anthony Elleray

Anthony Elleray 2 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The comparison should be easy enough once you've collected the product IDs to compare. I'd start there.

how would you store the ID when products are on multiple pages / pagination ?

is the below correct

perch_shop_products([
    'template' => 'products/list.html',
    'count' => 10,
    'filter' => 'slug',
    'match' => 'in',
    'value' => 'half-day-coasteering-hccoa001, mountiain-biking-2-day-hcmou002'

]);

i have got a comma separated string containing item slugs and want to display the items

Drew McLellan

Drew McLellan 2638 points
Perch Support

What result are you getting?

its only displaying the first one but found the issue

in my string there is a space after each comma that i need to remove

if anyone's interested i made my product comparison using cookies

in my product template i use this

<div class="button" data-name="product_<perch:shop id="sku" type="hidden" />" data-id="<perch:shop id="slug" type="slug" />">Compare</div>


$( ".button" ).each(function() {

    var id = $(this).attr("data-id");
    var name = $(this).attr("data-name");

    $(this).click(function(e){ //on add input button click
        document.cookie = ''+ name +'='+ id +'';
    });
});

then on the comparison page i used this

$cookie_name = 'product_';
foreach ($_COOKIE as $name => $value) {
    if (stripos($name,$cookie_name) === 0) {
        $cookies[] = $value;
    }
}
$sliced_array = array_slice($cookies, 0, 3);
$mystring = implode(',',$sliced_array);

echo $mystring;


perch_shop_products([
    'template' => 'products/list.html',
    'count' => 10,
    'filter' => 'slug',
    'match' => 'in',
    'value' => $mystring

]);

but if anyone has a better idea