我目前正在准备一个基于Woocommerce的在线商店,但是我有一个迷你推车的外观问题。每当特定产品的名称太长时,就会导致mini-cart出现问题(不适合.cart_wrapper)。
我决定隐藏(重复)产品名称中最不重要的元素。我使用了以下代码:
function wpse_remove_shorts_from_cart_title( $product_name ) {
$product_name = str_ireplace( 'premium', '', $product_name );
$product_name = str_ireplace( 'standard', '', $product_name );
return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'wpse_remove_shorts_from_cart_title' );
效果很好。以产品名称为例:
Car Carpet VW (1999-2001) - PREMIUM
我:Car Carpet VW (1999-2001) -
现在我的问题是出现在产品名称末尾的中间破折号。
我不能使用上面描述的方法删除,因为通过这种方式,它也删除了括号内的中间破折号(分隔年份或生产的那个)。
由于我的PHP知识非常基础-我转向您的问题-是否有任何标签允许我隐藏名字末尾的中间破折号,同时在括号之间保留现有的中间破折号。
我该怎么做呢?
是的,这是可能的与本地php函数 rtrim()
。你可以这样使用它:
<?php
$string1 = 'Car Carpet VW (1999-2001) - PREMIUM';
$string2 = 'Car Carpet VW (1999-2001) -';
$string1 = rtrim($string1, ' -');
$string2 = rtrim($string2, ' -');
echo '$string1: '.$string1.'<br>'; // displays "Car Carpet VW (1999-2001) - PREMIUM"
echo '$string2: '.$string2.'<br>'; // displays "Car Carpet VW (1999-2001)"
?>
引用: PHP函数空白()
为什么不直接用PREMIUM或STANDARD替换功能替换它呢?
:
function wpse_remove_shorts_from_cart_title( $product_name ) {
$product_name = str_replace( '- premium', '', $product_name );
$product_name = str_replace( '- standard', '', $product_name );
return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'wpse_remove_shorts_from_cart_title' );
我也会使用str_replace()
而不是str_ireplace()
,因为str_replace()
不区分大小写。