Search code examples
phpwordpresswoocommerceproducthook-woocommerce

Remove double quotes from add to cart message in WooCommerce


I am trying to find out how to remove or replace the double quotes from the WooCommerce add to cart message.

“UNF #10 x 1/4″” has been added to your CART.

Here is my Notice

I am dealing with some Imperial Measurements, and the double quotes look untidy surrounding the " for inch in the Product Name.

I have added filters at every stage of the message display

  • my last attempt is shown below
  • but I've also tried

add_filter ( 'wc_add_to_cart_message_html', 'wc_add_to_cart_message_html_filter', 10, 2 );

I have tried the find string

  • as "“"
  • and escaped "\“"
  • and as Unicode

I can replace any other part of the message.

But I can't remove or replace those pesky quotes.

add_filter ( 'woocommerce_add_success', 'woocommerce_add_success_filter', 10, 1 );
function woocommerce_add_success_filter ( $message ) {

    $message = str_replace("basket","CART", $message);
    $message = str_replace("\“","[ ", $message);

    return $message;
}

Any help or clues?


Solution

  • Use the woocommerce_add_to_cart_item_name_in_quotes hook to remove double quotes from the added to cart messages.

    Replace this part

    _x( '“%s”', 'Item name in quotes', 'woocommerce' )
    

    With

    _x( '%s', 'Item name in quotes', 'woocommerce' )
    

    So you get:

    function filter_woocommerce_add_to_cart_item_name_in_quotes( $item_name, $product_id ) {
        // Item name 
        $item_name = sprintf( _x( '%s', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
        
        return $item_name;
    }
    add_filter ( 'woocommerce_add_to_cart_item_name_in_quotes', 'filter_woocommerce_add_to_cart_item_name_in_quotes', 10, 2 );