在下拉菜单中按产品分组 WooCommerce 下载

问题描述 投票:0回答:1

我使用 WooCommerce 在我的网站上销售在线课程。每个课程(产品)都有许多可下载的文件。 不幸的是,WooCommerce 不会将每个产品的下载分组在一起并列出一长串可用下载。如果有人购买多门课程,将会出现一个非常长且混乱的下载列表,这对用户来说并不友好。

我正在寻找一些代码(PHP、Java、CSS)来将每个产品下载分组到下拉菜单下。这样,如果有人购买 7 门课程,那么在下载页面中将出现 7 个下拉列表,通过单击每个下拉列表,就会出现该特定产品的下载。

我使用了下面的 PHP 代码。它将下载分组在一起,但没有下拉菜单。

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


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

此外,此代码还会导致“我的订单”和“订单报告”页面中出现错误,导致无法显示下载。错误是这样的:

警告:第 156 行 /home/beatop/domains/sdrecords.ir/public_html/wp-content/themes/hello-theme-child-master/functions.php 中未定义的数组键“list” 没有下载文件

第156行是这样的:

$lists = $download['list'];

如何纠正此错误并添加下拉菜单?

php jquery woocommerce grouping product
1个回答
1
投票

更新:
您需要首先检查

$download['list']
是否存在,以避免出现该问题。

要获取按产品分组的下载下拉列表,需要进行一些更改和添加(需要 JavaScript/jQuery)

注意:电子邮件通知上,由于下拉菜单无法工作,我们用指向客户我的帐户“下载”部分的链接替换了下载表内容。

以下代码将处理此下拉列表任何地方,仅在前端

add_filter( 'woocommerce_customer_get_downloadable_products', 'prefix_group_downloadable_products', 10, 2 );
add_filter( 'woocommerce_order_get_downloadable_items', 'prefix_group_downloadable_products', 10, 2 );
function prefix_group_downloadable_products( $downloads = array(), $order = null ) {
    if ( is_admin() ) { 
        return $downloads;
    }

    $unique_downloads = []; // Initializing

    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_action( 'woocommerce_account_downloads_column_download-file', 'customize_downloads_columns' );
function customize_downloads_columns( $download = array() ) {
    $lists = isset($download['list']) ? $download['list'] : array();

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

    echo '<select class="downloads-dropdown">
    <option value="">'. __(' Select download', 'storefront' ) .'</option>';

    foreach ( $lists as $list ) {
        printf( '<option value="%s">%s</option>', 
        esc_url($list['download_url']), esc_html($list['file_name']) );

    }
    echo '</select>';
}

add_action( 'woocommerce_email_order_details', 'customized_email_order_downloads', 5 );
function customized_email_order_downloads() {
    $emails = WC()->mailer();
    remove_action( 'woocommerce_email_order_details', array( $emails, 'order_downloads' ), 10, 4 );
    add_action( 'woocommerce_email_order_details', 'email_order_downloads_custom_table', 9, 4 );
}

function email_order_downloads_custom_table( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );
    
    if ( ! $show_downloads ) {
        return;
    }

    $text_align = is_rtl() ? 'right' : 'left';
    $downloads_url = wc_get_endpoint_url('downloads', '', get_permalink( get_option('woocommerce_myaccount_page_id') ) );
    ?>
    <h2 class="woocommerce-order-downloads__title"><?php esc_html_e( 'Downloads', 'woocommerce' ); ?></h2>
    <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; margin-bottom: 40px;" border="1">
        <tr>
            <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;">
                <?php printf( __('Downloads are available in your Account %s.', 'storefront'),
                '<a href="' . $downloads_url . '" class="button">'. __('"Downloads" section', 'storefront') .'</a>'); ?>
            </td>
        </tr>
    </table>
    <?php 
}

add_action( 'woocommerce_account_downloads_endpoint', 'custom_downloads_dropdown_js', 20 );
add_action( 'woocommerce_account_view-order_endpoint', 'custom_downloads_dropdown_js', 20 );
add_action( 'woocommerce_thankyou', 'custom_downloads_dropdown_js', 20 );
function custom_downloads_dropdown_js() {
    wc_enqueue_js("$(document.body).on('change', 'select.downloads-dropdown', function(){
        if( $(this).val() != '' ) {
            window.location.href = $(this).val();
        }
    });");
}

您将得到类似 的内容(在我的帐户 > 下载页面):

在我的帐户>查看订单和收到的订单(谢谢)页面:

通过电子邮件通知(订购并下载)

© www.soinside.com 2019 - 2024. All rights reserved.