WooCommerce - Remove variation from product title in order details and email

喜欢而已 提交于 2019-12-13 20:40:13

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/52556402/woocommerce-remove-variation-from-product-title-in-order-details-and-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!