Woocommerce从结帐/购物车页面中删除产品类别链接

时间:2018-08-27 15:45:20

标签: php wordpress woocommerce cart hook-woocommerce

我正在购物车和结帐页面中显示Woocommerce产品类别。但是,我试图让它仅显示文本,并防止它被“ clickable” -即删除链接。

我尝试使用浏览器工具进行检查并使用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;

}

以下是当前输出: enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用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标记''来允许标签跨越。