1

我在 WooCommerce“我的帐户”>“下载”上的 woocommerce 下载部分有问题:在产品变体名称上,有一个<span>可见的 html 标签:

https://i.stack.imgur.com/TIg0X.png

我尝试使用以下方法删除那些“跨度”标签:

add_filter( 'woocommerce_customer_available_downloads', 'remove_span_dl_name', 10, 7);
function remove_span_dl_name( $download ){
    return str_replace( '<span> - </span>', ' - ',$download['download_name']);
}

但它完全删除了所有下载。

我还尝试使用以下方法删除那些“跨度”标签:

add_filter( 'woocommerce_available_download_link', 'remove_span_dl_name', 10, 7);
function remove_span_dl_name( $download ){
    return str_replace( '<span> - </span>', ' - ',$download['download_name'] );
}

我的错误是什么,我怎样才能摆脱这些标签?

4

1 回答 1

0

自 WooCommerce 版本 2.6 以来,该挂钩woocommerce_available_download_link包含在myaccount/my-downloads.php已弃用的模板中(使用的正确模板文件是myaccount/downloads.php)。

现在由于通过WC_Customer方法加载下载get_downloadable_products()您可以使用woocommerce_customer_get_downloadable_products此方法中包含的过滤器挂钩。或者也woocommerce_customer_available_downloads过滤钩。

要从产品名称中删除标签,您可以使用str_replace()或更好更高效strip_tags()

1)。第一种方式- 使用strip_tags()功能:

add_filter( 'woocommerce_customer_get_downloadable_products', 'remove_span_tags_from_product_name' );
// Or also
// add_filter( 'woocommerce_customer_available_downloads', 'remove_span_tags_from_product_name' );
function remove_span_tags_from_product_name( $downloads ){
    // Only on my account downloads section
    if ( ! is_wc_endpoint_url('downloads') )
        return $downloads;

    // Loop though downloads
    foreach( $downloads as $key => $download ) {
        // remove "span" html tags
        $downloads[$key]['product_name'] = strip_tags( $download['product_name'] );
    }
    return $downloads;
}

2)。另一种方式- 使用str_replace()功能:

add_filter( 'woocommerce_customer_get_downloadable_products', 'remove_span_tags_from_product_name' );
// Or also
// add_filter( 'woocommerce_customer_available_downloads', 'remove_span_tags_from_product_name' );
function remove_span_tags_from_product_name( $downloads ){
    // Only on my account downloads section
    if ( ! is_wc_endpoint_url('downloads') )
        return $downloads;

    // Loop though downloads
    foreach( $downloads as $key => $download ) {
        // remove "span" html tags
        $downloads[$key]['product_name'] = str_replace( array('<span>', '</span>'), array('', ''), $download['product_name'] );
    }
    return $downloads;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。任何方式都应该有效。

如果您希望它在任何地方都可以使用(“订单视图”、“已收到订单”、电子邮件通知),请删除:

// Only on my account downloads section
if ( ! is_wc_endpoint_url('downloads') )
    return $downloads;
于 2020-08-10T00:09:18.443 回答