Woocommerce 从 checkout/cart 页面中删除产品类别 link

Woocommerce remove product category link from checkout/cart page

我在购物车和结帐页面中显示 Woocommerce 产品类别。但是我试图让它只显示文本并防止它成为 "clickable" - 即删除 link.

我曾尝试使用浏览器工具进行检查并使用 CSS 进行删除,但没有成功。 - 感谢任何帮助!

这是我用于在结帐页面中显示类别的代码:

add_filter( 'woocommerce_cart_item_name', 'bbloomer_cart_item_category', 99, 3);

function bbloomer_cart_item_category( $name, $cart_item, $cart_item_key ) {

$product_item = $cart_item['data'];

// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
} 

$cat_ids = $product_item->get_category_ids();

// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );

return $name;

}

这是当前输出:

您可以使用 strip_tags() 函数从 PHP 中的字符串中删除所有 HTML 标记。在您的情况下,您只需要删除 a 标签。所以你这样做:

if ( $cat_ids ) $name .= '</br>' .strip_tags( wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' ),'<span>');

注意 strip_tags '' 的第二个参数以允许标记跨度。