Woocommerce产品下载URL

问题描述 投票:-1回答:1

我正在为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而且我不确定如何实现它。

任何指针将非常感谢!

php wordpress woocommerce
1个回答
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比学习它的基础知识要难得多:)

© www.soinside.com 2019 - 2024. All rights reserved.