2

I'm trying adding some files to downloadable product in magento below in my code.

   $linkfile = array('/magento-dev/media/downloadable/files/link_samples/');
            $samplefile = array();
            $_highfilePath = "/files/link_samples/";
            $_samplefilePath = "/files/link_samples/";
            $paths = array('highurl'=>$_highfilePath,'sampleurl'=>$_samplefilePath);

            $samplefile[] = array(
                    'file' => $_samplefilePath,
                    'name' => 'me.jpg',
                    'size' => '9999',
                    'status' => 'new'
            );

            $linkfile[] = array(
                    'file' => $_highfilePath,
                    'name' => 'me.jpg',
                    'size' => '9999',
                    'status' => 'new'
            );
    $linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
                    Mage_Downloadable_Model_Link::getBaseTmpPath(),
                    Mage_Downloadable_Model_Link::getBasePath(),
                    $linkfile
            );
        $linkModel = Mage::getModel('downloadable/link')->setData(array(
                            'product_id' => $product->getId(),
                            'sort_order' => 0,
                            'number_of_downloads' => 0, // Unlimited downloads
                            'is_shareable' => 2, // Not shareable
                            'link_url' => '',
                            'link_type' => 'file',
                            'link_file' => json_encode($linkfile),
                            'sample_file' => json_encode($samplefile),
                            'sample_type' => 'file',
                            'use_default_title' => false,
                            'title' => 'downloadable link',
                            'default_price' => 0,
                            'price'=>0,
                            'store_id'=>$storeId,
                            'website_id'=>$product->getStore()->getWebsiteId(),
                    ));

                    $linkModel->setLinkFile($linkFileName)->save();

I'm not getting what wrong with this I tried finding actual code on magento forums but no luck.

Any idea how to get it work?

Thanks

1 Answer 1

1

Ravi,please try below code ,it have been testing in system...

    require 'app/Mage.php';

    if (!Mage::isInstalled()) {
        echo "Application is not installed yet, please complete install wizard first.";
        exit;
    }

    // Only for urls
    // Don't remove this
    $_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
    $_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

    Mage::app('admin')->setUseSessionInUrl(false);

    umask(0);

    try {    


$downloadableProductId=239;
        $filesPath ="C:\wamp\www\magento1702V2\media\images";
    $items = array(
        'small' => array(
            'link' => array(
                'title' => 'Test file',
                'price' => '123',
                'is_unlimited' => '1',
                'number_of_downloads' => '111',
                'is_shareable' => '0',
                'sample' => array(
                    'type' => 'file',
                    'file' =>
                    array(
                        'filename' => 'files/test.txt',
                    ),
                    'url' => 'http://localhost/magento1702V2/media/img.jpg',
                ),
                'type' => 'file',
                'file' =>
                array(
                    'filename' => 'files/test.txt',
                ),
                'link_url' => 'http://localhost/magento1702V2/media/img.jpg',
            ),
            'sample' => array(
                'title' => 'Test sample file',
                'type' => 'file',
                'file' => array(
                    'filename' => 'files/image.jpg',
                ),
                'sample_url' => 'http://localhost/magento1702V2/media/img.jpg',
                'sort_order' => '3',
            )
        ),
        'big' => array(
            'link' => array(
                'title' => 'Test url',
                'price' => '123',
                'is_unlimited' => '0',
                'number_of_downloads' => '111',
                'is_shareable' => '1',
                'sample' => array(
                    'type' => 'url',
                    'file' => array(
                        'filename' => 'files/book.pdf',
                    ),
                    'url' => 'http://localhost/magento1702V2/media/img.jpg',
                ),
                'type' => 'url',
                'file' => array(
                    'filename' => 'files/song.mp3',
                ),
                'link_url' => 'http://localhost/magento1702V2/media/img.jpg',
            ),
            'sample' => array(
                'title' => 'Test sample url',
                'type' => 'url',
                'file' => array(
                    'filename' => 'files/image.jpg',
                ),
                'sample_url' => 'http://localhost/magento1702V2/media/img.jpg',
                'sort_order' => '3',
            )
        )
    );

    foreach ($items as $item) {
        foreach ($item as $key => $value) {
            if ($value['type'] == 'file') {
                $filePath = $filesPath . '/' . $value['file']['filename'];
                $value['file'] = array('name' => str_replace('/', '_', $value['file']['filename']), 
                'base64_content' => '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAXABcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDLooor8XP4DCiiigAooooAKKKKAP/Z'



                , 'type' => $value['type']);
            }
            if ($value['sample']['type'] == 'file') {
                $filePath = $filesPath . '/' . $value['sample']['file']['filename'];
                $value['sample']['file'] = array('name' => str_replace('/', '_', $value['sample']['file']['filename']), 
                'base64_content' => '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAXABcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDLooor8XP4DCiiigAooooAKKKKAP/Z'

                );
            }
            echo "<pre>";
            print_r($value);
            echo "</pre>";
             echo "<pre>";
            print_r($key);
            echo "</pre>";
            echo "<br/>....";
             Mage::getModel("downloadable/link_api")->add($downloadableProductId, $value, $key);

        }
    }

    } catch (Exception $e) {
        Mage::printException($e);
    }

I have decode my file in base64_encode.

2
  • Thanks amit for your help :-) I found a mistake in my code and solved the issue.
    – Ravi Soni
    Mar 16, 2014 at 5:54
  • oks..........please put your code
    – Amit Bera
    Mar 16, 2014 at 6:58

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