在 WooCommerce 中,我尝试将产品类别作为一个类应用于我的购物车中的商品。这就是这样做的,但它会干扰修改购物车值的能力 - “更新购物车”按钮停止工作。删除它意味着更新按钮再次起作用。
function filter_woocommerce_cart_item_class( $string, $cart_item, $cart_item_key ) {
// Get the product categories slugs for this item
$term_slugs = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array( 'fields' => 'slugs' ) );
// NOT empty
if ( ! empty ( $term_slugs ) ) {
$string = implode( ' ', $term_slugs );
}
return $string;
}
add_filter( 'woocommerce_cart_item_class', 'filter_woocommerce_cart_item_class', 10, 3 );
任何有关如何缓解该问题的建议表示赞赏。
使用 使用(或基于)产品类别将 CSS 类添加到 WooCommerce 购物车项目中的答案 - 我会直接对该帖子发表评论,但我的声誉不超过 50。
您很可能在此处删除已设置的类,该类用于通过仅返回您自己的新类来选择将“添加到购物车”功能绑定到客户端的元素。
不要覆盖
$string
,它已经包含了元素在修改之前应获取的类 - 而是追加到它。
if ( ! empty ( $term_slugs ) ) {
$string .= ' ' . implode( ' ', $term_slugs );
}