在时事通讯SubscriberController Magento中获取可下载的产品示例链接


Get downloadable product sample link in newsletter SubscriberController Magento

在我的Magento商店中,我添加了可下载的产品,如果用户想下载此产品,则需要订阅我们的时事通讯。为此,我在view.phtml文件中添加了时事通讯订户块。代码如下。

<?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 }     ?>

我已经设置了onclick功能,当用户点击这个按钮时,时事通讯弹出窗口就会打开。

在时事通讯PHTML文件中,我使用以下代码获得当前产品的id:

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

现在我们转到SubscriberController.php文件,它是newAction()。我使用以下代码在newAction()中获取当前产品id。

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

现在我想要使用这个产品Id是:

1) 。我想要关于当前产品的所有数据。我得到这是使用以下代码:

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

2) 。如果这个产品是可下载的,那么我想要它的样本数据和下载产品的链接,但我无法获得当前产品的下载链接。我获取下载链接的代码如下。

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

当我运行上面的代码时,它会进入其他部分,并返回"这里什么都没得到…"。

获取这些链接有点复杂。我准备了一个小片段如何获得示例文件和正常链接。

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;
                }
            }
        }
    }
}

您需要加载downloadable/sampledownloadable/link的集合,并向其传递产品模型或产品id