It seems to be trivial, but it’s not. If you want to use custom table and custom model, and sort collection by one of your table’s column, you can’t just use addAttributeToSort as you do with native Magento collection (like “catalog/product”). Instead of this, you should use setOrder, so your code should look like this:

$items = Mage::getModel('module/model')
         ->getCollection()
         ->setOrder('date_added', 'DESC')
         ->addFieldToFilter('category_id', array('eq' => $category_id))
         ->setPageSize($pageSize)
         ->setCurPage($currentPage);

 

In case someone will need this, it’s quite simple:

$order = Mage::getModel('sales/order')->load( 1234 );
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(array());
$shipment->register();
$shipment->getOrder()->setCustomerNoteNotify(false);
$shipment->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($shipment->getOrder())->save();

 

Preparing such export is quite easy, so let’s begin with adding new button in products listing:

in app/code/core/Mage/Adminhtml/Block/Catalog/Product.php in _prepareLayout() function add such code:

$this->_addButton('export_fact', array(
    'label'   => Mage::helper('catalog')->__('FACT-Finder'),
    'onclick' => "setLocation('{$this->getUrl('*/*/exportfact')}')",
    'class'   => 'add'
));

then in app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php add new function:

public function exportfactAction()
{
    $productCollection = Mage::getModel('catalog/product')->getCollection();

    $csv = '"ProductID";"ProductName";"Description";"Price"'."\n";

    foreach ($productCollection as $item)
    {
        $data = $item->getData();

        $product = Mage::getModel('catalog/product')->load($data['entity_id']);

        $csv.= '"'.sprintf("%09d", $product->getId()).'";"'.$product->getName().'";"'. strip_tags($product->getShortDescription() ).'";"'.sprintf("%.2f", $product->getPrice()).'"'."\n";
    }
    $this->_prepareDownloadResponse('fact.csv', $csv);
}

and thats all. You will get new button with export ability.

Today I had to change Magento default products list sorting to sort by product’s date. In fact we don’t need to sort by date, just by entity_id which is product ID in Magento world. To accomplish this, we need to edit /app/code/core/Mage/Catalog/Block/Product/List.php

You have to find _beforeToHtml() function and change some things inside. First we need to define all available sorting options:

$orders = array('entity_id' => $this->__('Newest'), 'name' => $this->__('Name'), 'price' => $this->__('Price') );

then remove all getters of default sorting and order, and put those:

$toolbar->setAvailableOrders($orders);
$toolbar->setDefaultOrder('entity_id');
$toolbar->setDefaultDirection('desc');

so finally your _beforeToHtml() should looks like this one:

protected function _beforeToHtml()
{

    $toolbar = $this->getToolbarBlock();

    // called prepare sortable parameters
    $collection = $this->_getProductCollection();

    // use sortable parameters
    $orders = array('entity_id' => $this->__('Newest'), 'name' => $this->__('Name'), 'price' => $this->__('Price') );
    $toolbar->setAvailableOrders($orders);

    $toolbar->setDefaultOrder('entity_id');

    $toolbar->setDefaultDirection('desc');

    if ($modes = $this->getModes()) {
        $toolbar->setModes($modes);
    }

    // set collection to tollbar and apply sort
    $toolbar->setCollection($collection);

    $this->setChild('toolbar', $toolbar);
    Mage::dispatchEvent('catalog_block_product_list_collection', array(
        'collection'=>$this->_getProductCollection(),
    ));

    $this->_getProductCollection()->load();
    Mage::getModel('review/review')->appendSummary($this->_getProductCollection());
    return parent::_beforeToHtml();
}

 

Maybe someone will need it, so here we go with code:

echo "<pre>";
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();
$categories = array();
if ($ids)
{
    foreach ($ids as $id)
    {
        $category->load($id);
        $categories[$id]['name'] = $category->getName();
        $categories[$id]['path'] = $category->getPath();
    }
    foreach ($ids as $id)
    {
        $path = explode('/', $categories[$id]['path']);
        $string = '';
        foreach ($path as $pathId)
        {
            $string.= $categories[$pathId]['name'] . ' > ';
            $cnt++;
        }
        $string.= ';' . $id . "\n";

        echo $string;
    }
}

 

the snippet above will give you such output:

root category 1 ; 1
root category 1 > subcategory 1 ; 2
root category 1 > subcategory 1 > subsubcategory 1 ; 3
root category 1 > subcategory 1 > subsubcategory 2 ; 4
root category 1 > subcategory 2 ; 5
root category 1 > subcategory 2 > subsubcategory 1 ; 6