A few questions around the sort product function

Open to all! Ask other shopowners for help.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: A few questions around the sort product function

Post by Portman »

Thankyou @Kofod95 & @14Steve14 ...

I will play a little bit more with the code that you mentioned @Kofod95 and if I cant get anything to work then, yes I'll probably need to reach out to a developer.


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: A few questions around the sort product function

Post by Portman »

Hello @Kofod95,

I took your advice and started playing around with the stuff you pointed me to and I have ALMOST got the bestsellers Hook working... here is the hook...

Code: Select all

<?php

class hook_shop_siteWide_sortableBestSeller{

  public function listen_filterStart($parameters) {
    $parameters['column_specifications']['PRODUCT_LIST_BESTSELLER'] = [
      'order_by' => " ORDER BY products_ordered DESC",
      'heading' => 'Bestsellers',
      'sortable' => true,
    ];

    uksort($parameters['column_specifications'], function ($a, $b) {
      return (constant($a) <=> constant($b));
    }); 

  }

}
Which actually sorts by best seller when you select it (best first) .... the only problem is it lists it from Best seller to least if you click the link again (ie it does not swap it)... also in the first instance the arrow is pointing up (is this the logical direction for best first or should it be the other way), and when you click it again the arrow changes to down but the sort order stays the same

I actually think it would be better to have no arrow at all and have it just stay in the best to least order regardless... but this won't help me when I start to work on the Date added Option...

So my question is 2 fold;
1. How do I remove the arrow?
2. How do I make it so the order flips when the sort order is selected a second time? I assume the line

Code: Select all

      'order_by' => " ORDER BY products_ordered DESC",
is forcing it to stay Best to least.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: A few questions around the sort product function

Post by ecartz »

Code: Select all

      'order_by' => " ORDER BY products_ordered%s",
That will make it so it switches, starting least to most. To switch it to start most to least, you have to check if it's the currently sorted column and then flip it. Something like

Code: Select all

if (BESTSELLER_COLUMN_INDEX == $parameters['sort_column']) {
  $parameters['direction'] = ('a' === substr($_GET['sort'], -1)) ? ' DESC' : '';
}
Note that BESTSELLER_COLUMN_INDEX will not be set automatically. You'll either need to set it or replace it with the actual number or a variable that you set.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: A few questions around the sort product function

Post by Portman »

thanks @ecartz

What sort of value should BESTSELLER_COLUMN_INDEX be? is it the number of items per page? or columns per page? or something else entirely?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: A few questions around the sort product function

Post by ecartz »

It's the index of the $column_list

When you have &sort=1a in the URL, the 1 is one more than the index. So if it's 1 in the URL, it would be 0 as the sort_column. If it were 5 in the URL, it would be 4 as the sort_column. Since you can reorder the columns and hide them, it's going to be different in every store.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: A few questions around the sort product function

Post by Portman »

ok, I have obvioulsy done something wrong,

this is how my hook now looks;

Code: Select all

<?php

class hook_shop_siteWide_sortableBestSeller{

  public function listen_filterStart($parameters) {
    $parameters['column_specifications']['PRODUCT_LIST_BESTSELLER'] = [
      'order_by' => " ORDER BY products_ordered%s, products_name",
      'heading' => 'Bestsellers',
      'sortable' => true,
    ];

if (BESTSELLER_COLUMN_INDEX == $parameters['sort_column']) {
  $parameters['direction'] = ('a' === substr($_GET['sort'], -1)) ? ' DESC' : '';
}

    uksort($parameters['column_specifications'], function ($a, $b) {
      return (constant($a) <=> constant($b));
    }); 

  }

}
where sort=4 in URL so BESTSELLER_COLUMN_INDEX = 3

It now just defaults least to most - and then swaps to most to least which was what the original sort order did that got me trying to change it in the first place
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: A few questions around the sort product function

Post by Kofod95 »

What if you turn this upside down:

Code: Select all

if (BESTSELLER_COLUMN_INDEX == $parameters['sort_column']) {
  $parameters['direction'] = ('a' === substr($_GET['sort'], -1)) ? ' DESC' : '';
}
So it becomes

Code: Select all


if (BESTSELLER_COLUMN_INDEX == $parameters['sort_column']) {
  $parameters['direction'] = ('d' === substr($_GET['sort'], -1)) ? ' ASC' : '';
}
?

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: A few questions around the sort product function

Post by Portman »

No Sorry that made no difference...

Code: Select all

<?php

class hook_shop_siteWide_sortableBestSeller{

  public function listen_filterStart($parameters) {
    $parameters['column_specifications']['PRODUCT_LIST_BESTSELLER'] = [
      'order_by' => " ORDER BY products_ordered%s, products_name",
      'heading' => 'Bestsellers',
      'sortable' => true,
    ];

if (BESTSELLER_COLUMN_INDEX == $parameters['sort_column']) {
  $parameters['direction'] = ('d' === substr($_GET['sort'], -1)) ? ' ASC' : '';
}

    uksort($parameters['column_specifications'], function ($a, $b) {
      return (constant($a) <=> constant($b));
    }); 

  }

}
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: A few questions around the sort product function

Post by ecartz »

Code: Select all

  $parameters['direction'] = ('d' === substr($_GET['sort'], -1)) ? '' : ' DESC';
Also add

Code: Select all

    $parameters['column_orderings']['PRODUCT_LIST_BESTSELLER'] = " ORDER BY products_ordered%s, products_name";
    $parameters['column_list'][$parameters['sort_column']] = 'PRODUCT_LIST_BESTSELLER';
If that doesn't work, try rewriting it as

Code: Select all

if ('d' === substr($_GET['sort'], -1)) {
  $parameters['direction'] = '';
  $_GET['sort'][-1] = 'a';
} else {
  $parameters['direction'] = ' DESC';
  $_GET['sort'][-1] = 'd';
}
    uksort($parameters['column_orderings'], function ($a, $b) {
      return (constant($a) <=> constant($b));
    });
If none of that works, you're going to have to look further into what isn't working.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply