将产品文件下载 Link 添加到感谢页面 WooCommerce
Add Product File Download Link to Thank You Page WooCommerce
我正在使用此代码,以便客户可以在购买完成后直接从感谢页面下载文件。问题是,它给我一个错误,说“WP_Hook->apply_filters(NULL, Array)
”
这是我使用的代码:
add_action( 'woocommerce_thankyou', 'add_download_link_to_thank_you_page' );
function add_download_link_to_thank_you_page() {
$downloads = $product->get_files();
foreach( $downloads as $key => $each_download ) {
echo '<a href="'.$each_download["file"].'">Download Item</a>';
}}
不明白这是怎么回事
您的代码中未定义变量 $product
,函数中缺少挂钩参数 $order_id
。
您还需要使用 WC_Order
get_downloadable_items()
更高效的订单处理方法。
Normally the downloadable items are displayed in a specific table before the order details (when there is downloadable items, depending on permissions settings), so it's very strange that you are trying to display them otherwise.
所以试试下面的方法:
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page' );
function add_download_links_to_thank_you_page( $order_id ) {
$order = wc_get_order( $order_id );
$html = [];
if( $downloads = $order->get_downloadable_items() ) {
foreach( $downloads as $download ) {
$html[] = '<a href="'.$download["file"]['file'].'">' . __('Download') . ' "' . $download["file"]['name'] . '"</a>';
}
}
if( ! empty($html) ){
echo implode('<br>', $html);
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
要在订单详情 table 之前显示下载,您将替换:
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page' );
来自
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page', 10, 5 );
我正在使用此代码,以便客户可以在购买完成后直接从感谢页面下载文件。问题是,它给我一个错误,说“WP_Hook->apply_filters(NULL, Array)
”
这是我使用的代码:
add_action( 'woocommerce_thankyou', 'add_download_link_to_thank_you_page' );
function add_download_link_to_thank_you_page() {
$downloads = $product->get_files();
foreach( $downloads as $key => $each_download ) {
echo '<a href="'.$each_download["file"].'">Download Item</a>';
}}
不明白这是怎么回事
您的代码中未定义变量 $product
,函数中缺少挂钩参数 $order_id
。
您还需要使用 WC_Order
get_downloadable_items()
更高效的订单处理方法。
Normally the downloadable items are displayed in a specific table before the order details (when there is downloadable items, depending on permissions settings), so it's very strange that you are trying to display them otherwise.
所以试试下面的方法:
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page' );
function add_download_links_to_thank_you_page( $order_id ) {
$order = wc_get_order( $order_id );
$html = [];
if( $downloads = $order->get_downloadable_items() ) {
foreach( $downloads as $download ) {
$html[] = '<a href="'.$download["file"]['file'].'">' . __('Download') . ' "' . $download["file"]['name'] . '"</a>';
}
}
if( ! empty($html) ){
echo implode('<br>', $html);
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
要在订单详情 table 之前显示下载,您将替换:
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page' );
来自
add_action( 'woocommerce_thankyou', 'add_download_links_to_thank_you_page', 10, 5 );