phpmagentomagento-1.9product

Get downloadable product sample link in newsletter SubscriberController Magento


In my Magento store I have added downloadable products and if user want to download this product user need to subscribe our newsletter. for that I have added newsletter subscriber block in view.phtml file. Code is below.

<?php $download = Mage::registry('current_product')->getTypeId();?>
            <?php 
                if($download == 'downloadable')
                { ?>
                
                <div class="pop-upss">
                    <input type="button" onclick="showMrnlePopup();" value="To Download This Product You Need To Subscribe Our Newsletter" name="submit"/>
                </div>
            <?php }     ?>

I have set onclick function and when user will click on this button newsletter pop-up will open.

In newsletter PHTML file I get current product's id using below code:

<input type="hidden" name="pro_id" value="<?php echo Mage::registry('current_product')->getId();?>">

Now we go to SubscriberController.php file and it's newAction(). I get current product id in newAction() using below code.

$product_id = $this->getRequest()->getPost('pro_id');

Now what I want using this product Id is:

1). I want all data about current product. I got this is using below code:

if($product_id) {
    $obj = Mage::getModel('catalog/product');
    $_product = $obj->load($product_id);
    
    echo '<pre>';
    print_r($_product->getData());
    die; 
}

2). if this product is downloadable then I want its sample data and its link to download product but I cannot get current product's download link. My code for get download link is below.

if($_product->hasSamples()) {
    $_samples = $this->getSamples();
    
    foreach ($_samples as $_sample){
         echo $samplePath = $_sample->getBasePath();
         echo $sampleFile = $_sample->getSampleFile();
    }
} else {
    echo 'not getting anything here...';
}

When I run above code it goes into else part and echo 'not getting anything here...'.


Solution

  • Getting these links is a bit complicated. I prepared a little snippet how to get the sample files and the normal links.

    public function indexAction()
    {
    
        $product_id = $this->getRequest()->getPost('pro_id');
    
        if($product_id) {
            /** @var Mage_Catalog_Model_Product $product */
            $product = Mage::getModel('catalog/product')->load($product_id);
    
            if($product->getTypeId() == 'downloadable') {
                /** @var Mage_Downloadable_Model_Resource_Sample_Collection $samples */
                $samples = Mage::getModel('downloadable/sample')->getCollection()->addProductToFilter($product_id);
    
                if($samples->count() > 0) {
                    foreach($samples as $sample) {
                        /** @var Mage_Downloadable_Model_Sample $sample */
                        $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/samples' . $sample->getSampleFile();
                        echo 'Sample URL: ' . $url . "<br>";
                    }
                }
    
                /** @var Mage_Downloadable_Model_Resource_Link_Collection $links */
                $links = Mage::getModel('downloadable/link')->getCollection()->addProductToFilter($product);
    
                if($links->count() > 0) {
                    foreach($links as $link) {
                        /** @var Mage_Downloadable_Model_Link $link */
                        $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/links' . $link->getLinkFile();
                        echo 'Link URL: ' . $url;
                    }
                }
            }
        }
    }
    

    You need to load collections of downloadable/sample and downloadable/link and pass a product model or a product id to it. That's the Magento way ;-)