简体   繁体   English

用管道替换 WooCommerce 购物车项目变体标题中的某个字符

[英]Replace a certain character in WooCommerce cart item variations title by a pipe

How to remove a comma separation from product attribute on my checkout.如何在我的结帐时从产品属性中删除逗号分隔。

Example:例子:

Blue Black Raw Denim - 37, Slimfit

37 is attribute product and slimfit is model product I want remove comma or replace with | 37是属性产品, slimfit是模型产品我想删除逗号或替换为| . . how can I do that?我怎样才能做到这一点?

For cart code like this :对于这样的购物车代码:

<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 );
      }

If I delete this part item product name and attribute will gone but instead gone.如果我删除这部分项目,产品名称和属性将消失,而是消失了。
I want replace comma with a |我想用|替换逗号... ...

How can I achieve this?我怎样才能做到这一点?

Thanks谢谢

Reference Image:参考图片: 在此处输入图片说明

So you should use a custom function hooked in woocommerce_cart_item_name filter hook, where you will replace the coma by a pipe using str_replace() php function, this way:因此,您应该使用挂钩在woocommerce_cart_item_name过滤器钩子中的自定义函数,您将在其中使用str_replace() php 函数通过管道替换昏迷,这样:

// 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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

This code is tested and works.此代码经过测试并有效。 It will replace the coma by a pipe in cart and checkout items title.它将用购物车和结帐项目标题中的管道替换昏迷。


Related answer: WooCommerce 3.0 - hide variation info in product title相关答案: WooCommerce 3.0 - 在产品标题中隐藏变化信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM