how to add default products to related products list

You can do this with the help of a custom observer method. To do so, call your observer's function at event catalog_product_save_after.

By using this, whenever a product will be saved from admin panel, you can check whether the product has related products assigned or not.

If it has no related products, then you can assign related products (by product ids) programatically.

Suppose the product in which you want to assign related products has id 2 and related ids are 1,5,7 and 3.

Then:

$product = Mage::getModel('catalog/product')->load(2);
if($product)
{
    $sRelatedIds = '1,5,7,3';
    $aRelatedIds = explode(',', $sRelatedIds);
    $aParams = array();
    $nRelatedCounter = 1;

    foreach($aRelatedIds as $id)
    {
        $aRelatedProduct = Mage::getModel('catalog/product')->load($id);
        $aParams[$aRelatedProduct['entity_id']] = array('position' => $nRelatedCounter);
        $nRelatedCounter++;
    }

    $product->setRelatedLinkData($aParams);
    $product->save();
}

Please let me know if you have any questions.


You can write an observer for catalog_product_collection_load_after, then add products to the loaded collection if the collection is the related products collection:

use Mage_Catalog_Model_Product as Product;
use Mage_Catalog_Model_Product_Link as RelatedProduct;
use Mage_Catalog_Model_Resource_Product_Link_Product_Collection as RelatedProductCollection;

class IntegerNet_AutoRelated_Model_Observer
{
    /**
     * @see event catalog_product_collection_load_after
     * @param Varien_Event_Observer $observer
     * @throws Mage_Core_Exception
     */
    public function addToRelatedCollection(Varien_Event_Observer $observer)
    {
        $collection = $observer->getCollection();
        if ($collection instanceof RelatedProductCollection
            && $collection->getLinkModel()->getLinkTypeId() === RelatedProduct::LINK_TYPE_RELATED
        ) {
            $this->addItems($collection);
        }
    }

    protected function addItems(RelatedProductCollection $collection)
    {
        /** @var Mage_Catalog_Model_Resource_Product_Collection $productsToAdd */
        $productsToAdd = Mage::getResourceModel('catalog/product_collection');
        $productsToAdd
            ->addStoreFilter()
            ->addIdFilter(array_diff([1,5,7,3], [$collection->getProduct()->getId()], $collection->getAllIds()))
            ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->setPageSize($numberOfItems)
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addUrlRewrite();
        foreach ($productsToAdd as $product) {
            $collection->addItem($product);
        }
    }
}

Some parts that I'd like to highlight:

->addIdFilter(array_diff([1,5,7,3], [$collection->getProduct()->getId()], $collection->getAllIds()))

This loads the products [1,5,7,3] but excludes the product itself and the products that are already manually defined as related products. Otherwise we would get an error due to duplicates in the collection. You probably want to move these hard coded ids to a configuration.

->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();

This prepares the product collection to load the necessary data to display prices, the product link and any attributes configured as "used in product listing", but not more.

This is a very similar solution as in my AutoUpsell module described in Get product list by category id in view.phtml, I copied most of the code from there with slight modifications.