Jump to content

[Resolved] Product sort order in admin.


violinman

Recommended Posts

Hello, is there any way to set admin products to always display in product ID order rather than having to click that column every time to set the order?

On a similar note, my product ID numbers run from WP01 through to WP160, the trouble is even when I click on the product ID column to sort it will for example put in order WP01 to WP10 but then continues with WP100, I realise this is logical to the program but I wondered if there is anyway to change this, I don't really want to add an extra 0 to the low numbers so that they are all three digits wide.

Any advice much appreciated.

Brian

 

Link to comment
Share on other sites

Well, probably not by using PHP. To do so would require loading all records, sorting in memory, then slicing the result into pages.

CubeCart has the database restrict its output to "slices".

So, we need to figure out how to code the call to DB->select() to pass in the desired custom ordering, which is a bit tricky.

Link to comment
Share on other sites

It looks like admin/source/products.index.inc.php has an area concerning Sorting:

	// Sorting
	$current_page = currentPage(array('sort'));
	if (!isset($_GET['sort']) || !is_array($_GET['sort'])) {
		$_GET['sort'] = array('updated' => 'DESC');
	}
	$thead_sort = array (
		'status'   => $GLOBALS['db']->column_sort('status', $lang['common']['status'], 'sort', $current_page, $_GET['sort']),
		'digital'   => $GLOBALS['db']->column_sort('digital', $lang['common']['type'], 'sort', $current_page, $_GET['sort']),
		'image'   => $lang['catalogue']['title_image'],
		'name'    => $GLOBALS['db']->column_sort('name', $lang['catalogue']['product_name'], 'sort', $current_page, $_GET['sort']),
		'product_code'  => $GLOBALS['db']->column_sort('product_code', $lang['catalogue']['product_code'], 'sort', $current_page, $_GET['sort']),
		'price'   => $GLOBALS['db']->column_sort('price', $lang['common']['price'], 'sort', $current_page, $_GET['sort']),
		'stock_level'  => $lang['catalogue']['title_stock'],
		'updated'   => $GLOBALS['db']->column_sort('updated', $lang['catalogue']['title_last_updated'], 'sort', $current_page, $_GET['sort']),
		'translations'  => $lang['translate']['title_translations']
	);

Not sure if that helps you or not. Hopefully Bsmither will be back on for a bit.

Of course, please be sure to keep a clean backup copy before fiddling. Not sure if you would need to clear CC and browser cache for admin changes or not, but always a good idea.

 

Link to comment
Share on other sites

thank you for that,

i did find that sort section but I think for it to work on what I have I would need to use PHP string functions to remove the letters WP, then sort the numbers and then add back the WP at the front of the string assuming it will correctly sort 01 through to 160 without thinking 100 comes after 10!

Brian

 

Link to comment
Share on other sites

That is one way to do it, but if the Product Code is standardized and pervasive, then one approach I have seen used is to give the query a:

SORT BY length_of_string, string

This groups Codes by number of characters: single digit (ones), double digit (tens), triple digit (hundreds), then sorts within each group.

In the file products.index.inc.php, near line 1108 (CC617), CubeCart uses the value found in $_GET['sort'] and sends that array directly to the query. Sending an array requires the array key being a valid database table column. So, to pass something other than a table column will require us to use a string. And that will require a bit of editing in this file. We will need to construct a derived string only if the sort is on product_code.

Also, we will need to intercept the use of product_code and swap it to LENGTH(product_code), product_code

then change the query code in line 1152 to use the derived string from $_GET['sort'].

Link to comment
Share on other sites

The interesting thing is that if I look at the rows in the data base the sort is perfect! phpMyAdmin can do it.

Bsmither I get the drift of your thinking but I am afraid it is a bit beyond my capabilities, I can make changes to php files if I know exactly where to put it but your explanation assumes an understanding that I probably am not up to, it has to be simple for me!

Brian

Link to comment
Share on other sites

Let's make this first change: making CubeCart have the initial sort be by product_code, even if the sort is not 'natural'. We can work on that later.

In the file products.index.inc.php:

Find near lines 1108-1112 (for CC617):

	// Sorting
	$current_page = currentPage(array('sort'));
	if (!isset($_GET['sort']) || !is_array($_GET['sort'])) {
		$_GET['sort'] = array('updated' => 'DESC');
	}

Change to:

	// Sorting
	$current_page = currentPage(array('sort'));
	if (!isset($_GET['sort']) || !is_array($_GET['sort'])) {
		$_GET['sort'] = array('product_code' => 'ASC');
	}

 

Link to comment
Share on other sites

Have made the change you suggested, that is so much better, many thanks for that. I hope to hear from you if you manage to solve the number sort problem. I was looking at other posts online of people with a similar problem, it seems what is needed is a lexicongraphical sort.

Could I have your name?

Many thanks,

Brian

Link to comment
Share on other sites

Ok, so if that works, then we know that edits are being successfully applied.

In the same file:

Near line 1152, find:

	if (($where === false || strlen($where) > 0) && ($results = $GLOBALS['db']->select('CubeCart_inventory', false, $where, $_GET['sort'], $per_page, $page)) !== false) {


Change to:

	if (($where === false || strlen($where) > 0) && ($results = $GLOBALS['db']->select('CubeCart_inventory', false, $where, ((key($_GET['sort']) == "product_code") ? "LENGTH(`product_code`) ".$_GET['sort']['product_code'].", `product_code` ".$_GET['sort']['product_code'] : $_GET['sort']), $per_page, $page)) !== false) {

This change looks for a sort on product_code and changes what gets sent to the database function.

(Unfortunately, I do not a means to test this right now, so be prepared to restore the file from a backup.)

Link to comment
Share on other sites

Great! You will need to make these kinds of edits available on subsequent CC upgrades.

I put a comment before and after such changes, so I might end up with something that looks like this:

{* STOCK 6.1.7 LINE REPLACED BY BSMITHER FOR PRODUCT CODE SORT
 if (($where === false || strlen($where) > 0) && ($results = GLOBALS['db']->select('CubeCart_inventory', false, $where, $_GET['sort'], $per_page, $page)) !== false) { *}
{* NEW BSMITHER PRODUCT CODE SORT *}
if (($where === false || strlen($where) > 0) && ($results = $GLOBALS['db']->select('CubeCart_inventory', false, $where, ((key($_GET['sort']) == "product_code") ? "LENGTH(`product_code`) ".$_GET['sort']['product_code'].", `product_code` ".$_GET['sort']['product_code'] : $_GET['sort']), $per_page, $page)) !== false) {
{* END BSMITHER PRODUCT CODE SORT *}

Commenting each code edit makes adding them into the next upgrade version much easier.

Link to comment
Share on other sites

You are absolutely right, I have spent this morning listing all of the mods I have made, I do comment as I go along, the large percentage of mods are to the skin files but also some to admin.

The thought of upgrading is a bit of a nightmare, I will only do that when the time comes if it is a major feature improvement or a security upgrade. once it is working I don't want to break it!! I reckon around another three or four weeks and it will be ready to go live, I can then switch the DNS from the current live site which is a ClickCartPro site but very old, I built it over 15 years ago!

Thanks for all the help and suggestions, I will be back for more as I am not there yet!

Brian

 

Link to comment
Share on other sites

Quote

The thought of upgrading is a bit of a nightmare, I will only do that when the time comes if it is a major feature improvement or a security upgrade. once it is working I don't want to break it!! 

Many people would agree with you. But I find it less confusing to make small changes all along from GitHub commits on a test site.

I use file compare software to merge upgrade changes with my edited version.

Link to comment
Share on other sites

That is a really good tip! I used to do C programming many years ago and in those days I did use a file compare program which was brilliant. I had completely forgotten about the file compare. I think that is probably the best way to go, can you tell me the name of your file compare software please?

Many thanks,

Brian

Link to comment
Share on other sites

I recommend Git. While it may have a steeper learning curve than some other software, it is a full on version control system, not just a file comparison tool.

For example, to update to the latest version, I simply run the following Git command:

git pull upstream master

Now my store is up-to-date and still includes all of my customizations - all I have to do from here is FTP the files to my live server.

Link to comment
Share on other sites

I would agree that @bsandall is right about Git. But I was already used to BeyondCompare and found changing to Git was more trouble for me than I had time for the learning curve. But if you are starting this journey from scratch, it makes sense to learn the best.

Link to comment
Share on other sites

Thank you both for the recommendations, Git does look a bit daunting, I did install it but have not had a chance to see if it is something I have time to learn. Presumably I can do the same thing with beyond compare but would need to select manually what mods I want to keep?

Brian

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...