将WooCommerce购物车项目变体标题中的某个字符替换为竖线

2022-04-03 00:00:00 product php wordpress woocommerce cart

如何在结账时从产品属性中删除逗号分隔。

示例:

Blue Black Raw Denim - 37, Slimfit

37是属性产品,slimfit是我要删除逗号或替换为|的模型产品。我如何才能做到这一点?

对于这样的购物车代码:

<td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
    <?php
      if ( ! $product_permalink ) {
        echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;';
      } else {
        echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key );
      }

如果删除此部件项,产品名称和属性将消失,而不是消失。
我希望将逗号替换为|...

如何实现此目标?

谢谢

参考图像:


解决方案

所以您应该使用woocommerce_cart_item_name中挂接的自定义函数,其中您将使用str_replace()php函数将Coma替换为管道,这样:

// Tested on WooCommerce version 3+
add_filter( 'woocommerce_cart_item_name', 'custom_item_name', 10, 3 );
function custom_item_name( $item_name, $cart_item, $cart_item_key ){
    $new_item_name = str_replace(',', ' |', $item_name);
    return $new_item_name;
}

代码位于活动子主题(或主题)的函数.php文件或任何插件文件中。

这段代码已经过测试,并且运行正常。它将用购物车和结账项目标题中的管道替换Coma。


相关答案:WooCommerce 3.0 - hide variation info in product title

相关文章