在 WooCommerce 购物车页面上显示购物车项目自定义下载 URL

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

我要求客户为添加到购物车的每个产品上传一个 PDF 文件,并且需要显示每个购物车项目的文件下载 URL。 WooCommerce 似乎删除了 HTML 标签并且不允许显示 URL。我所看到的只是每个购物车项目的“下载”标签。从购物车查看源代码没有显示任何URL。

这是我的插件文件:

add_filter('woocommerce_get_item_data', 'display_custom_data_in_cart', 10, 2);
function display_custom_data_in_cart($item_data, $cart_item) {
    if (!empty($cart_item['rx_type'])) {
        $item_data[] = array(
            'name' => 'RX Type Details',
            'value' => $cart_item['rx_type']
        );
        error_log('RX Type Details: ' . $cart_item['rx_type']);
    }
    if (!empty($cart_item['calculations'])) {
        $item_data[] = array(
            'name' => 'Cost per Lens',
            'value' => '$' . number_format($cart_item['calculations'], 2)
        );
        error_log('Calculations: ' . $cart_item['calculations']);
    }
    if (!empty($cart_item['uploaded_file_url'])) {
        $item_data[] = array(
            'name' => 'Uploaded Prescription',
            'value' => esc_html($cart_item['uploaded_file_url']) // Display the URL as plain text
        );
        error_log('Uploaded Prescription URL: ' . $cart_item['uploaded_file_url']);
    } else {
        error_log('No uploaded file URL found in cart item data.');
    }
    return $item_data;
}

我尝试删除 cart-item-data.php 模板文件中的 wp_kses_post 函数调用,但没有任何区别。

如何在 WooCommerce 购物车页面中显示购物车项目的下载 URL?

php url woocommerce hook-woocommerce cart
1个回答
0
投票

要在 WooCommerce 购物车页面中显示自定义 URL,请尝试以下操作:

function get_item_custom_data(){
    return array(
        'rx_type'           => __('RX Type Details', 'woocommerce'),
        'calculations'      => __('Cost per Lens', 'woocommerce'),
        'uploaded_file_url' => __('Uploaded Prescription', 'woocommerce'),
    );
}

add_action( 'woocommerce_after_cart_item_name', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item, $cart_item_key ) {
    foreach (get_item_custom_data() as $key => $label_name ) {
        if ( isset($cart_item[$key]) && !empty($cart_item[$key]) ) {
            if ( $key === 'uploaded_file_url' ) {
                printf( '<span class="custom-data %s"><strong>%s</strong>: <a href="%s">%s</a></span>', 
                $key, $label_name, esc_url($cart_item[$key]), __('Download Url', 'woocommerce') );
            } else {
                printf( '<span class="custom-data %s"><strong>%s</strong>: <span>%s</span></span>', 
                $key, $label_name, $key === 'calculations' ? wc_price($cart_item[$key]) : esc_attr($cart_item[$key]) );
            }
        }
    }
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

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