Woocommerce产品下载URL

时间:2018-01-04 16:20:53

标签: php wordpress woocommerce

我为The Grid插件制作了一个皮肤(https://theme-one.com/docs/the-grid/#developer_guide)。

所有产品都是虚拟和可下载的 - 我已设法制作添加到购物车的功能,但也需要立即下载产品。是否存在文件URL的元键(梳理数据库并且无法找到数据库)或获取它的方法?

目前我有一些东西只能返回一个数组:

$output .= '<p><a href="' . $tg_el->get_item_meta('_downloadable_files') . '" download target="_blank"><i class="fa fa-download"></i> Download Image</a></p>';

我以为我找到了一个可能的解决方案:https://wordpress.stackexchange.com/a/199884/134263但是使用php并不是很好,而且我不确定如何实现它。

任何指针都会非常感激!

1 个答案:

答案 0 :(得分:0)

可下载的Woocommerce是阵列,正如您在后端可以看到的,它为您提供了添加多个可下载的选项。

假设您在可以启动全局变量$product的产品模板上,您可以使用方法$product->get_files()获取其他帖子所提及的产品可下载列表,这将给出你是一个可下载文件数组,你必须循环遍历每个数组项以获取名称和链接等数据,你必须使用foreach循环。

我不知道你想在哪里添加这个,但根据你的代码,你可以做这样的事情,

// Declaring $product variable from woocommerce, this isn't needed if previously declared
global $product;

// get all downloadable files from the product
$files = $product->get_files(); 

//Loop through each downloadable file
foreach( $files as $file ) {
    //store the html with link and name in $output variable assuming the $output variable is declared above
    $output .= '<p><a href="' . $file['file'] . '" download target="_blank"><i class="fa fa-download"></i> '. $file['name'].'</a></p>';
}

然后应该将链接添加到您正在使用的$ output变量echoreturn

另外,尝试学习基本的php,它并不那么难,在没有知识的情况下使用php比学习它的基础要困难得多:)

相关问题