We have a system that populates a Magento installation by reading an external database and creating, updating and deleting products and categories. Recently I had been receiving an obscure message when trying to update categories using the Magento API.
The message was simply: Default Product Listing Sort by not exists on Available Product Listing Sort By
After Googling around I hadn’t turned up much, which forced me into the bowels of Magento. This exception is thrown in the file app/code/core/Mage/Model/Category/Attribute/Backend/Sortby.php on line 81 and 85. This file is responsible for checking that the Sort By method specified for a category is valid.

Default Display Settings for a new cateogry
When you create a category in Magento using the Admin UI, the values in the Display Settings tab of the category as set as per this screenshot (click to enlarge). The default values are:
- Available Product Listing Sort By = Use All Available Attributes
- Default Product Listing Sort By = Use Config Settings
With this category created, I used the following test script to check to see if I could update the category. This script simply asks for the information about the category (category 4066 in my case) and attempts to update the category with that information.
<?php
define('MAGENTO', getcwd() );
define('CATEGORY_ID', 4066 );
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
try
{
$info = Mage::getModel('catalog/category_api')->info( CATEGORY_ID );
var_dump( $info );
Mage::getModel('catalog/category_api')->update( CATEGORY_ID, $info );
echo "\n\nSUCCESS\n\n";
}
catch( Exception $e )
{
var_dump( $e );
}
?>
When I ran this with the default settings, I received a “data_invalid” exception, however tweaking it to use any of the values in the select box for the Default Product Listing Sort By resulted in the exception “Default Product Listing Sort by not exists on Available Product Listing Sort By” (tip: it was useful to pipe the output of this script to `less’). I then had a test script that I could use to verify when my problem was fixed.
I then fiddled around with the display settings in Magento (which by the way does not suffer from this problem when saving a category) and found that if I set the Available Product Listing Sort By to anything other than using the tick to use all available attributes, and set the Default Product Listing Sort By to anything other than using the tick to use the Config Settings, then things magically started working.
This eventually led to me reworking a bit of code to include specific values for the available_sort_by and default_sort_by attributes when updating the category. Here’s my bit of code – out of context, but you get the idea.
$result = $api->update
(
$category_id,
array
(
'image' => $image_path,
'description' => $description,
'meta_description' => $meta_description,
'meta_keywords' => $meta_keywords,
'available_sort_by' => array( 'name' ),
'default_sort_by' => 'name'
)
);
Good luck!