1
votes

I have a variation called "China" on some of my products. The way WooCommerce works, is that inside the cart, order details and emails, the variations are being appended to the product title. I would like to have the word "China" removed from the title in the cart, checkout, order details and email.

Current Title: "Apple MacBook Pro 15 - China, 16GB Ram, 512GB SSD"

Required Title: "Apple MacBook Pro 15 - 16GB Ram, 512GB SSD"

I managed to remove it from the title when it's inside the cart and checkout page, using this code:

function remove_variation_from_product_title( $title, $cart_item, $cart_item_key ) {
    $_product = $cart_item['data'];

    if ( $_product->is_type( 'variation' ) ) {

        $replace = array( 'China,', 'China' );
        $title = str_replace( $replace, '', $title);

        return apply_filters( 'woocommerce_product_variation_title', $title );
    }

    return $title;
}
add_filter( 'woocommerce_cart_item_name', 'remove_variation_from_product_title', 10, 3 );

But as you can see from the filter, it only removes it from the cart. Once the order is made, the full title with variations are being shown again in the order details page as well as in the email that is sent from the customer.

I did come across another filter called woocommerce_order_items_meta_display, but failed to get anywhere with it.

Does anyone know how I could filter the product title across the entire store, and not just in the cart and checkout page?

Thanks.


Update:

Found the solution. woocommerce_order_item_name did the trick. It also applies it to the email that is sent to the customer.

function remove_variation_from_product_title_order( $title, $item, $is_visible ) {

    $_product = $item['data'];
    $replace = array( 'China,', 'China' );
    $title = str_replace( $replace, '', $title);

    return apply_filters( 'woocommerce_product_variation_title', $title );

}
add_filter( 'woocommerce_order_item_name', 'remove_variation_from_product_title_order', 10, 3 );
2
search for woocommerce_update_order_item_meta it will helpraju_odi

2 Answers

1
votes

Found it. woocommerce_order_item_name did the trick. It also applies it to the email that is sent to the customer.

function remove_variation_from_product_title_order( $title, $item, $is_visible ) {

    $_product = $item['data'];
    $replace = array( 'China,', 'China' );
    $title = str_replace( $replace, '', $title);

    return apply_filters( 'woocommerce_product_variation_title', $title );

}
add_filter( 'woocommerce_order_item_name', 'remove_variation_from_product_title_order', 10, 3 );
0
votes

I'm working on a similar problem and stumbled upon woocommerce_order_item_name as well. In my case, the WooCommerce order notification emails are displaying the product title with variant (much like your example) but also re-listing the attribute name (used to create the variations - in this case "Submission Type") along with the product title with variant below for each product, like so:

Online Application - Full Application

Submission Type: Full Application

Does anyone know how exactly the output is created for this and if applying a filter to woocommerce_order_item_name can modify the output so that the the second line specifying the variation is removed?

I struggled to find out what exactly can be passed to woocommerce_order_item_name as arguments and how the variation name is being output. How did you find this?

Right now I have a start and seem to be able to add a condition for the matching products I want to modify based on the main product title and separator prefix used at the start of the product, but not sure how to find/alter the variation appearing on the second line in the emails:

add_filter( 'woocommerce_order_item_name', 'edit_order_item_name');

    function edit_order_item_name($item) {

        $prefix = 'Online Application - ';

        if (substr($item, 0, strlen($prefix)) == $prefix) {

        // do something here to remove the variation type and name from appearing below the product name

        }

        else {
        return $item;
    }
}