Php WooCommerce-按产品分组下载
在Woocommerce(*我的帐户页)中,我现在可以看到一个无序列表,其中包含所有可用下载,如:Php WooCommerce-按产品分组下载,php,html,wordpress,woocommerce,digital-downloads,Php,Html,Wordpress,Woocommerce,Digital Downloads,在Woocommerce(*我的帐户页)中,我现在可以看到一个无序列表,其中包含所有可用下载,如: <ul class="digital-downloads"> <li><a href="#">Product 1 - File</a></li> <li><a href="#">Product 1 - Another File</a></li> <li><a h
<ul class="digital-downloads">
<li><a href="#">Product 1 - File</a></li>
<li><a href="#">Product 1 - Another File</a></li>
<li><a href="#">Product 2 - File</a></li>
<li><a href="#">Product 2 - Another File</a></li>
<li><a href="#">Product 3 - File</a></li>
<li><a href="#">Product 3 - Another File</a></li>
</ul>
如何按产品对下载进行分组?例如:
<ul class="digital-downloads">
<li>
<span>Product 1</span>
<ul>
<li><a href="#">File</a></li>
<li><a href="#">Another File</a></li>
</ul>
</li>
<li>
<span>Product 2</span>
<ul>
<li><a href="#">File</a></li>
<li><a href="#">Another File</a></li>
</ul>
</li>
<li>
<span>Product 3</span>
<ul>
<li><a href="#">File</a></li>
<li><a href="#">Another File</a></li>
</ul>
</li>
</ul>
-
产品1
-
产品2
-
产品3
我的主题/woocommerce/my account/my-downloads.php中的代码:
<ul class="digital_downloads">
<?php foreach ( $downloads as $download ) : ?>
<li>
<?php
do_action( 'woocommerce_available_download_start', $download );
echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $download['download_name'] . '</a>', $download );
do_action( 'woocommerce_available_download_end', $download );
?>
</li>
<?php endforeach; ?>
</ul>
-
我知道,我很晚才回答这个问题,但我会发布一个答案,以防其他人正在寻找答案
请创建一个子主题,并在该子主题中创建文件/woocommerce/my account/my-downloads.php。在该文件中添加以下内容
<?php
/**
* My Orders
*
* Shows recent orders on the account page
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.0.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
function wdm_print_download_file_name( $download, $product_meta ) {
if ( is_numeric( $download['downloads_remaining'] ) )
echo apply_filters( 'woocommerce_available_download_count', '<span class="count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download );
echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $product_meta[$download['download_id']]['name'] . '</a>', $download ); //Print file name
}
if ( $downloads = WC()->customer->get_downloadable_products() ) :
?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_downloads_title', __( 'Available downloads', 'woocommerce' ) ); ?></h2>
<ul class="digital-downloads">
<?php
$all_product_ids = array();
$all_download_ids = array();
$download_ids_used_till_now = array();
foreach ( $downloads as $download ) :
?>
<?php
do_action( 'woocommerce_available_download_start', $download );
if ( !in_array( $download['product_id'], $all_product_ids ) ) { //Check if current product id is already there in $all_product_ids array. If it goes in this loop, that means it is new product id
$product_meta = get_post_meta( $download['product_id'], '_downloadable_files', true ); //All download ids of the product
$all_product_ids[] = $download['product_id']; //Push Current download's Product id to an array
$all_download_ids = array_keys( $product_meta ); //Get all download ids of the current product
echo '<li><span>' . get_the_title( $download['product_id'] ) . '</span><ul>'; //Print product name
$download_ids_used_till_now[] = $download['download_id']; //add download id to an array
echo '<li>';
wdm_print_download_file_name( $download, $product_meta );
echo '</li>';
$check_if_this_is_last_element = array_values( array_diff( $all_download_ids, $download_ids_used_till_now ) );
if ( empty( $check_if_this_is_last_element ) ) { //This is last download id of a product
echo '</ul></li>'; //close ul and li tags
}
} else { //product id is already in use.
$check_if_this_is_last_element = array_values( array_diff( $all_download_ids, $download_ids_used_till_now ) );
if ( isset( $check_if_this_is_last_element[0] ) && $download['download_id'] == $check_if_this_is_last_element[0] ) { //This is last download id of a product
echo '<li>';
wdm_print_download_file_name( $download, $product_meta );
echo '</li>';
echo '</ul></li>'; //close ul and li tags
unset( $download_ids_used_till_now );
unset( $all_download_ids );
} else { //There are few more download ids
$download_ids_used_till_now[] = $download['download_id'];
echo '<li>';
wdm_print_download_file_name( $download, $product_meta );
echo '</li>';
}
}
do_action( 'woocommerce_available_download_end', $download );
?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i修复
改变
线路
if ( isset( $check_if_this_is_last_element[0] ) && $download['download_id'] == $check_if_this_is_last_element[0] )
到
上面的代码似乎过早地结束了子列表。因此,如果我在一个产品下有两次下载,它将显示为
<ul class="digital-downloads">
<li>
<span>Product 1</span>
<ul>
<li><a href="#">Product 1 File 1</a></li>
</ul>
</li>
<li><a href="#">Product 1 File 2</a></li>
</ul>
-
产品1
相反,我只是将html标记更改为divs。这在语义上可能不太正确,但它完成了任务,而且代码更简单
function wdm_print_download_file_name( $download, $product_meta ) {
if ( is_numeric( $download['downloads_remaining'] ) )
echo apply_filters( 'woocommerce_available_download_count', '<span class="count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download );
echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $product_meta[$download['download_id']]['name'] . '</a>', $download ); //Print file name
}
if ( $downloads = WC()->customer->get_downloadable_products() ) :
?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_downloads_title', __( 'Available Downloads and Videos', 'woocommerce' ) ); ?></h2>
<div class="digital-downloads">
<?php
$all_product_ids = array();
$all_download_ids = array();
foreach ( $downloads as $download ) :
?>
<?php
do_action( 'woocommerce_available_download_start', $download );
if ( !in_array( $download['product_id'], $all_product_ids ) ) { //Check if current product id is already there in $all_product_ids array. If it goes in this loop, that means it is new product id
$product_meta = get_post_meta( $download['product_id'], '_downloadable_files', true ); //All download ids of the product
$all_product_ids[] = $download['product_id']; //Push Current download's Product id to an array
$all_download_ids = array_keys( $product_meta ); //Get all download ids of the current product
echo '<div class="download-title">' . get_the_title( $download['product_id'] ) . '</div>'; //Print product name
} else {
//product id is already in use.
}
echo '<div class="download-product">';
wdm_print_download_file_name( $download, $product_meta );
echo '</div>';
do_action( 'woocommerce_available_download_end', $download );
?>
<?php endforeach; ?>
</div>
<?php endif; ?>
function wdm\u print\u download\u file\u name($download,$product\u meta){
如果(是数字($download['downloads\u remaining']))
echo应用过滤器('woocommerce'可用下载计数',''.sprintf('n('%s下载剩余','%s下载剩余',$download['downloads\u剩余'],'woocommerce'),$download['downloads\u剩余']),$download);
echo apply_filters('woocommerce_available_download_link',''$download);//打印文件名
}
如果($downloads=WC()->customer->get_downloadable_products()):
?>
谢谢事实上,即使这个问题有点老,我自己也没有弄明白,所以我会在接下来的几天里测试你的答案。今天测试,它工作顺利,正是我需要的。非常感谢。是否也可以添加产品的特色图片?如果是,怎么做?我想你不明白这个问题我没有同样的问题。我总是在一个产品下至少有两次下载,它们看起来都像你在第二个文件中显示的。
function wdm_print_download_file_name( $download, $product_meta ) {
if ( is_numeric( $download['downloads_remaining'] ) )
echo apply_filters( 'woocommerce_available_download_count', '<span class="count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download );
echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $product_meta[$download['download_id']]['name'] . '</a>', $download ); //Print file name
}
if ( $downloads = WC()->customer->get_downloadable_products() ) :
?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_downloads_title', __( 'Available Downloads and Videos', 'woocommerce' ) ); ?></h2>
<div class="digital-downloads">
<?php
$all_product_ids = array();
$all_download_ids = array();
foreach ( $downloads as $download ) :
?>
<?php
do_action( 'woocommerce_available_download_start', $download );
if ( !in_array( $download['product_id'], $all_product_ids ) ) { //Check if current product id is already there in $all_product_ids array. If it goes in this loop, that means it is new product id
$product_meta = get_post_meta( $download['product_id'], '_downloadable_files', true ); //All download ids of the product
$all_product_ids[] = $download['product_id']; //Push Current download's Product id to an array
$all_download_ids = array_keys( $product_meta ); //Get all download ids of the current product
echo '<div class="download-title">' . get_the_title( $download['product_id'] ) . '</div>'; //Print product name
} else {
//product id is already in use.
}
echo '<div class="download-product">';
wdm_print_download_file_name( $download, $product_meta );
echo '</div>';
do_action( 'woocommerce_available_download_end', $download );
?>
<?php endforeach; ?>
</div>
<?php endif; ?>