简体   繁体   中英

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 | . 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:

// 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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