We are doing custom Magento themes, Magento design, custom Magento extensions and creative services through Magento designers & developers for worldwide shop owners.

Last News
Update purchased downloadable product link for specific product programmatically

Update purchased downloadable product link for specific product programmatically

hi guys this tutorial is for quetsion : how update purchased downloadable product link for specific product programmatically, by example you have selling downloadable product for many client and you want change the link of this product , the problem all client have other old link and magento don’t update the new link you need find solution for that , Ok this code is for you :

First

got to config.xml inside etc and add the observer (more info about observer inside magento) :

1
2
3
4
5
6
7
8
9
10
11
<events>
            <catalog_product_save_after>
          <observers>
         <change_link>
                <type>singleton</type>
            <class>module/observer</class>
            <method>change_link</method>
        </change_link>
        </observers>
      </catalog_product_save_after>
    </events>

now you need go to folder Model and create the class Observer.php, the content of this class is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
class Package_Module_Model_Observer {

       public function change_link($observer) {
         $prd = $observer->getEvent()->getProduct();
         if($prd->getTypeId()=="downloadable")
         {
         $date = new DateTime();
         $prdPurItem = Mage::getModel('downloadable/link_purchased_item')->getCollection()
         ->addFieldToFilter('product_id',$prd->getId()); 
         if($prd->getTypeInstance(true)->hasLinks($prd)){
         $fl=$prd->getTypeInstance(true)->getLinks($prd);
         foreach($fl as $fl1){
         if(!is_null($prdPurItem))
         {
              foreach ($prdPurItem as $_itemPur) {
              $_itemPur->setLinkUrl(null)
               ->setLinkId($fl1["link_id"])
               ->setLinkType('file')
               ->setLinkTitle("Ready For Download")
               ->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE)
               ->setLinkFile($fl1["link_file"]) 
               ->setUpdatedAt($date->format('Y-m-d H:i:s')) 
               ->save();
               
              }
         }
         }
         }
         }
                
       }
}

now magento observe any change of link and update

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*