如何在WooCommerce的my-downloads.php中对多个产品链接进行分组? [英] How to Group Multiple Product Links in my-downloads.php for WooCommerce?

查看:58
本文介绍了如何在WooCommerce的my-downloads.php中对多个产品链接进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个答案的高低.似乎应该很容易.

I have searched high and low for this answer. Seems like it should be so easy.

在WooCommerce的用户帐户中列出购买产品的下载链接的基本设置是:

The basic set-up for listing a purchased product's download links in the user account in WooCommerce is:

echo apply_filters( 'woocommerce_available_download_link', '<a href="' . esc_url( $download['download_url'] ) . '">' . $download['download_name'] . '</a>', $download );

这最终在帐户中显示如下:

This ends up showing like this in the account:

基本上,它列出了产品名称,然后列出了包含的链接.如果每个产品要下载十个链接,则此列表会很长.然后,如果您购买了多个产品,每个产品都有十个链接,那么列表就很长了.我正在寻找的是一种使此列表看起来像这样的解决方案...并现在注意产品2从产品1开始的位置.

Basically, it lists the products name, followed by the included link. If you have ten links per product to download, then this list gets long.. Then, if you have multiple products purchased, each with ten links, it's just a long run on list. What I'm looking for is a solution to make this list look like this...and notice now where Product 2 starts after Product 1.

我很惊讶这个选项不是标准的,但是我找不到任何解决方案.我已经尝试了很多代码,以至于我什至都不知道从哪里开始.

I'm totally surprised this option isn't standard, but I can't find any solution. I've tried and tried...so much code that I don't even know where to start.

有人对这个WooCommerce问题有任何想法吗?

Anyone have any idea on this WooCommerce issue?

推荐答案

对于希望对可下载项目进行分组的任何人,通过 WC()-> customer-> get_downloadable_products()函数返回按产品ID.

For anyone out there who's looking to group downloadable items return by WC()->customer->get_downloadable_products() function by product ID.

下面的以下代码段修改了 WC()-> customer-> get_downloadable_products()函数的返回数据,以按产品ID对可下载文件进行分组.

This following below snippet modify the return data for the function WC()->customer->get_downloadable_products() to group downloadable files by product ID.

/**
 * Group Downloadable products by product ID
 *
 * @param array $downloads
 * @return array
 */
function prefix_group_downloadable_products( array $downloads ) {
    $unique_downloads = [];

    foreach ( $downloads as $download ) {
        $list = [
            'download_url' => $download['download_url'],
            'file_name'    => $download['file']['name']
        ];

        if ( array_key_exists( $download['product_id'], $unique_downloads ) ) {
            $unique_downloads[ $download['product_id'] ]['list'][] = $list;
            continue;
        }

        $data = $download;
        $data['list'] = [ $list ];
        $unique_downloads[ $download['product_id'] ] = $data;
    }

    return $unique_downloads;
}

add_filter( 'woocommerce_customer_get_downloadable_products', 
'prefix_group_downloadable_products' );

安装好过滤器后,请使用过滤器挂钩 woocommerce_account_downloads_column_download-file 修改布局,以在有序列表中列出可下载文件.

Once the filter is in place, use the filter hook woocommerce_account_downloads_column_download-file to modify the layout to list downloadable files in ordered list.

/**
 * Show number list of downloadable files for group product
 *
 * @param array $download
 * @return void
 */
function prefix_downloads_column_download_file( array $download ) {
    $lists = $download['list'];

    if ( empty( $lists ) ) {
        _e( 'No Download Files', 'storefront' );
        return;
    }

    echo '<ol>';

    foreach ( $lists as $list ) {
        echo '<li>';
        echo '<a href="' . esc_url( $list['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file">';
        echo esc_html( $list['file_name'] );
        echo '</a></li>';
    }

    echo '</ol>';
}

add_action( 'woocommerce_account_downloads_column_download-file', 'prefix_downloads_column_download_file' );

这里要注意的一点是,原始下载文件数组的数据结构未更改,我们刚刚引入了新的密钥 list [] ,该密钥将每个可下载文件的URL和名称保存在列表键.

One thing to note here, that the data structure of original download files array is unchanged, we have just introduced a new key list[] which holds each downloadable file URL and name in the list key.

这篇关于如何在WooCommerce的my-downloads.php中对多个产品链接进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆