0

I need to have that when saving a product it should automatically be set as related to its related products

UPDATED so it also removes the inverse related product when removed

0

I made a solution add an observer in etc

<global>
    <events>
        <catalog_product_save_before>
            <observers>
                <name_module>
                    <class>name_module/observer</class>
                    <method>prepareInverseRelation</method>
                </name_module>
            </observers>
        </catalog_product_save_before>
        <catalog_product_save_after>
            <observers>
                <name_module>
                    <class>name_module/observer</class>
                    <method>setInverseRelatedProducts</method>
                </name_module>
            </observers>
        </catalog_product_save_after>
    </events>
</global>

and then in Model/Obserrver.php put this

class Name_Module_Model_Observer extends Mage_Core_Model_Abstract
{
    public function setInverseRelatedProducts($observer){
        $product = $observer->getEvent()->getProduct();  
        $origid=$product->getId();
        $oldcompare = Mage::registry('oldcompare'.$origid);
        foreach($product->getRelatedProductCollection() as $relpr){
            $oldcompare = array_diff($oldcompare, [$relpr->getId()]);
            $related = Mage::getModel('catalog/product')->load($relpr->getId()); 
            $data = array();
            $save = true;
            foreach($related->getRelatedProductCollection() as $relrel){
                if ($relrel->getId() == $origid){$save = false;}
                $data[$relrel->getId()] = array('position'=>0);
            } 
            $data[$origid] = array('position'=>0);
            if ($save){$related->setRelatedLinkData($data)->save();}
        }
// this part removes the product from any related list that this product is no longer related to 
        if (!empty($oldcompare)){
          foreach($oldcompare as $remove){
            $removep = Mage::getModel('catalog/product')->load($remove); 
            $data2 = array();
            $save2 = false;
            foreach($removep->getRelatedProductCollection() as $rellist){
                if ($rellist->getId() == $origid){
                    $save2 = true;
                } else {$data2[$rellist->getId()] = array('position'=>0);}
            } 
            if ($save2){$removep->setRelatedLinkData($data2)->save();}
          }
        }
        Mage::unregister('oldcompare'.$origid); 
    }

    public function prepareInverseRelation($observer)
    {
        $product = $observer->getEvent()->getProduct();     
        $orriglist = array();
        foreach($product->getRelatedProductCollection() as $origr){
            $orriglist[] = $origr->getId();
        } 
        Mage::register('oldcompare'.$product->getId(), $orriglist);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.