I would like to edit a downloadable product by php. Specifically I would like to change/add the file url that would be used by the system to access the file to be purchased e.g. http://somewhere/song.mp3. I looked at an example online and this seems to be fairly simple for samples(this is not my code):

 $sampleModel = Mage::getModel('downloadable/sample')->setData(array(
                'product_id' => $product->getId(),
                'sort_order' => 0,
                'sample_url' => '/u/n/untitled_1.png',
                'sample_file' => '',
                'sample_type' => 'file',
                 'title' =>'mYtitle'

I don't know how to do this for the link url. And Where do I place the script. Thanks.

1 Answer 1


Create one sample php file on root and add this script in that file. You can load your specific product and set data you want to update. Run the file in browser.

require 'app/Mage.php';

$product = Mage::getModel('catalog/product')->load($productId);

if ($product->getTypeId() == "downloadable") {

    $date = new DateTime();

    $purchaseItems = Mage::getModel('downloadable/link_purchased_item')->getCollection()
            ->addFieldToFilter('product_id', $product->getId());

    if ($product->getTypeInstance(true)->hasLinks($product)) {

        $links = $product->getTypeInstance(true)->getLinks($product);
        foreach ($links as $_link)
                    ->setLinkTitle("Ready For Download")
                    ->setUpdatedAt($date->format('Y-m-d H:i:s'))
  • I am currently testing xampp. Does the system automatically set the link_id or I have to put a unique literal value here? Where is says "setLinkUrl", I noticed you have it set to null. Do I need to replace null with the url I intend to use?
    – megatr0n
    Commented Oct 2, 2015 at 3:10
  • Sir I am getting: Fatal error: Call to a member function load() on a non-object in ...
    – megatr0n
    Commented Oct 2, 2015 at 3:53
  • Sorry, there was an issue with Mage::getModel('catalog/product'). 'c' was a special character by mistake. I have updated entire script. It should work now. You can set data and check in admin for that product. Commented Oct 2, 2015 at 4:54
  • This is pure awesomeness. Cheers!. I wish I could help someone the way you helped me. Thanks a million
    – megatr0n
    Commented Oct 2, 2015 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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