为Woocommerce购物车添加溢出和标题标签



我需要添加

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

以及一个带有产品名称的标题标记(title=")到购物车页面上购物车表中的Woocommerce产品名称。如果我添加css到

.product-name a

将不起作用,产品缩略图不再显示。我如何添加css以及添加标题标签的链接?

这个CSS,你可能是从我的"WooCommerce:如何缩短产品标题"教程,将无法在购物车页面上工作,因为您在表格中。它将简单地将表列扩展到项目名称的长度,这是不理想的。

在这种情况下,您需要PHP,并且通过使用相同的引用,您可以测试以下自定义:

add_filter( 'woocommerce_cart_item_name', 'bbloomer_shorten_cart_item_title', 9999, 3 );

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

// GET PRODUCT
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

// GET PERMALINK
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

// ADD TITLE, LIMIT TO 15 CHARS AND ADD ELLIPSIS
return sprintf( '<a href="%s" title="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name(), substr( $_product->get_name(), 0, 15 ) . '...' );

}

谁发现截断的词标题..试试这个..它为我工作生成的chatgpt

add_filter( 'woocommerce_cart_item_name', 'bbloomer_shorten_cart_item_title', 9999, 3 );
function bbloomer_shorten_cart_item_title( $name, $cart_item, $cart_item_key ) {
// GET PRODUCT
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

// GET PERMALINK
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

// TRUNCATE TITLE BY WORDS
$title_words = explode( ' ', $_product->get_name() );
$max_words = 5; // Set the maximum number of words
$truncated_title = implode( ' ', array_slice( $title_words, 0, $max_words ) );
$ellipsis = count( $title_words ) > $max_words ? '...' : '';
// ADD TRUNCATED TITLE WITH ELLIPSIS
return sprintf( '<a href="%s" title="%s">%s%s</a>', esc_url( $product_permalink ), $_product->get_name(), $truncated_title, $ellipsis );
}

最新更新