php - Woocommerce 无需结帐即可免费下载
问题描述
我正在尝试使免费下载易于下载,而无需完成任何结帐或表格。我刚刚在 Github 上找到了这段代码,但我不知道如何添加它并让它工作。
function direct_free_downloads_button( $button )
{
global $product;
if( $product->is_downloadable() AND $product->get_price() == 0 )
{
$files = $product->get_files();
$files = array_keys($files);
$download_url = home_url('?download_file='.$product->id.'&key='.$files[0].'&free=1' );
$button = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
esc_url( $download_url ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? '' : '',
esc_attr( $product->product_type ),
esc_html( 'Download' )
);
}
return $button;
}
add_filter('woocommerce_loop_add_to_cart_link', 'direct_free_downloads_button', 100);
/**
* Handles downloading of free Downloadable products
* @return [type] [description]
*/
function download_free_product_file()
{
$product_id = absint( $_GET['download_file'] );
$_product = wc_get_product( $product_id );
if( $_product->get_price() == 0 ) {
WC_Download_Handler::download( $_product->get_file_download_path( filter_var($_GET['key'], FILTER_SANITIZE_STRING) ), $product_id );
}
}
if ( isset( $_GET['download_file'] ) && isset( $_GET['key'] ) && $_GET['free'] ) {
add_action( 'init', 'download_free_product_file' );
}
解决方案
如果它是可下载的产品,那么只需在产品页面中提供直接下载链接。并使用产品添加中的链接作为附属产品。
推荐阅读
- .net - 如何将字符串放入系统进程?
- javascript - 使用 websockets 直播 mp3
- ios - 使用 Arc4random 从列表中获取随机图像
- c# - 使用 for 循环填充列表
- html - 读取 Smil 文件
- python - GroupBy 对象内的数据处理。如何添加列?
- nginx - Nginx:避免 nginx:[警告] 在 0.0.0.0:80 上冲突的服务器名称“索引”,忽略
- sprite-kit - SpriteKit:绕一圈顺时针旋转船
- azure - 没有 Azure 应用服务计划的 Azure SQL 数据库?
- rust - 当关联类型没有大小时,如何避免需要`std::marker::Sized`?