1

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'
));
$sampleModel->save();

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

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.

<?php
require 'app/Mage.php';
Mage::app('default');


$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)
        {
            $_link->setLinkUrl('null')
                    ->setLinkId($_link["link_id"])
                    ->setLinkType('file')
                    ->setLinkTitle("Ready For Download")
                    ->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE)
                    ->setLinkFile($_link["link_file"])
                    ->setUpdatedAt($date->format('Y-m-d H:i:s'))
                    ->save();
        }
    }
}
| improve this answer | |
  • 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 Oct 2 '15 at 3:10
  • Sir I am getting: Fatal error: Call to a member function load() on a non-object in ... – megatr0n Oct 2 '15 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. – Akhilesh Patel Oct 2 '15 at 4:54
  • This is pure awesomeness. Cheers!. I wish I could help someone the way you helped me. Thanks a million – megatr0n Oct 2 '15 at 16:30

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.