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

Get downloadable product sample link in newsletter SubscriberController Magento

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

<?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).如果这个产品是可下载的,那么我想要它的示例数据,它是 link 来下载产品,但我无法获得当前产品的下载 link.my 下面是获取下载 link 的代码。

if($_product->hasSamples()) {
    $_samples = $this->getSamples();

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

当我 运行 上面的代码进入 else 部分并回显 'not getting anything here...'。 所以请任何知道如何获得可下载产品的人link请帮助我。

谢谢。

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

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 传递给它。这就是 Magento 的方式 ;-)