How to Group Multiple Product Links in my-download

2019-05-24 10:48发布

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

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:

enter image description here

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.

enter image description here

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.

Anyone have any idea on this WooCommerce issue?

1条回答
时光不老,我们不散
2楼-- · 2019-05-24 11:08

For anyone out there who's looking to group downloadable items return by WC()->customer->get_downloadable_products() function by product 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' );

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' );

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.

查看更多
登录 后发表回答