5

I want to add samples in downloadable product programmatically.

I am trying this

 $samplefiles[] = array(
                    'file' => '/u/n/untitled_1.png',
                    'name' => 'Untitled.png',
                    'size' => 4672,
                    'status' => 'new'
            );
 $sampleModel = Mage::getModel('downloadable/sample')->setData(array(
                    'product_id' => $product->getId(),
                    'sort_order' => 0,
                    'sample_url' => '',
                    'sample_file' => json_encode($samplefiles),
                    'sample_type' => 'url',
            ));
            $sampleModel->save();

But its not working..And how to save sample title

2

1 Answer 1

2

Your code does not save file at system.

As you have using url as sample type(sample_type) ('sample_type' => 'url')` that magento does not save file in system and it just save a link.

If you want to upload image to magento and want to set file as sample type ,then you need to follow below steps:

  1. Upload image using Mage_Core_Model_File_Uploader at media/downloadable/files/samples or move file at that location
  2. and set sample type and file location at sample_file

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();

enter image description here

And database:

enter image description here

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.