Changing Woocommerce Cart Item Names

Changing WooCommerce cart item names

The woocommerce_order_item_name filter hook is a front-end hook and is located in:

1) WooCommerce templates:

  • emails/plain/email-order-items.php
  • templates/order/order-details-item.php
  • templates/checkout/form-pay.php
  • templates/emails/email-order-items.php

2)WooCommerce Core file:

  • includes/class-wc-structured-data.php

Each of them has $item_name common first argument, and different for the other arguments.
See Here for more details…

You have 2 arguments set in your function (the second one is not correct for all templates) and you are declaring only one in your hook. I have tested the code below:

add_filter( 'woocommerce_order_item_name', 'change_orders_items_names', 10, 1 );
function change_orders_items_names( $item_name ) {
$item_name = 'mydesiredproductname';
return $item_name;
}

And it works on

  • Order-receive (Thank you) page,
  • email notifications
  • and My Account Orders > Single order details

But NOT in Cart, Checkout and Backend Order edit pages.

So if you need to make it work on Cart and Checkout, you should use other hooks like woocommerce_before_calculate_totals.
Then you can use WC_Product methods (setter and getters).

Here is your new code

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {

// Get an instance of the WC_Product object
$product = $cart_item['data'];

// Get the product name (Added Woocommerce 3+ compatibility)
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;

// SET THE NEW NAME
$new_name = 'mydesiredproductname';

// Set the new name (WooCommerce versions 2.5.x to 3+)
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}

Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

Now you have change the names everywhere except on Shop archives and product pages…

This code is tested and works on WooCommerce 2.5+ and 3+

If you want to keep original item names in cart only you should add this conditional WooCommerce tag inside the function:

if( ! is_cart() ){
// The code
}

This answer has been updated on August 1st 2017 to get a woocommerce compatibility prior versions…

Change the cart item name for product variations in Woocommerce

You will need to use a custom function hooked in woocommerce_cart_item_name filter hook this way:

add_filter( 'woocommerce_cart_item_name', 'custom_variation_item_name', 10, 3 );
function custom_variation_item_name( $item_name, $cart_item, $cart_item_key ){
// Change item name only if is a product variation
if( $cart_item['data']->is_type('variation') ){
// HERE customize item name
$item_name = __('my custom item name');

// For cart page we add back the product link
if(is_cart())
$item_name = sprintf( '<a href="%s">%s</a>', esc_url( $cart_item['data']->get_permalink() ), $item_name );
}
return $item_name;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and working

Add a line break to Woocommerce cart item names and order item names

To alter the product name display on cart items, minicart and order items (+ email notifications), use the following additional code:

// For mini cart and cart item name
add_action( 'woocommerce_cart_item_name', 'alter_wc_cart_item_name', 10, 2 );
function alter_wc_cart_item_name( $item_name, $cart_item ) {
if ( strpos($item_name , '|') !== false ) {
$item_name = str_replace( '|', '<br/>', $item_name );
}
return $item_name;
}

// For order item name (even on email notifications)
add_action( 'woocommerce_order_item_name', 'alter_wc_order_item_name', 10, 2 );
function alter_wc_order_item_name( $item_name, $item ) {
if ( strpos($item_name , '|') !== false ) {
$item_name = str_replace( '|', '<br/>', $item_name );
}
return $item_name;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Change cart item price and name for a specific product dynamically in Woocommerce

First, it's difficult to help as we don't know anything about your "studio" custom post type and how the data is set on it for the customers.

There are some mistakes and missing things in your code like in your IF statements, where you should need to use in_array() conditional function.

I have tried to guess how the data is set in wpcf-listing-option meta data for your studio custom post type and I suppose that is a number between 1 (one) and 7 (seven).

Also in your code, when you loop through $studio_query, for $studio_listing_name and $studio_listing_option you will always get the values from the last item from the loop… so there's something wrong in your logic.

In the following code I am targeting your specified product Id only (without any guaranty, it should help even if it doesn't work completely):

add_action( 'woocommerce_before_calculate_totals', 'customize_cart_item_details', 20, 1);
function customize_cart_item_details( $cart ) {

// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 || ! is_user_logged_in() )
return;

// ID of the product I've set up
$defined_product_id = 2938;

$customer_id = get_current_user_id();

// Get "studio" custom post objects
$studio_query = get_posts( array(
'post_type' => 'studio',
'post_status' => 'published',
'posts_per_page' => 1,
'author' => $customer_id
) );

// Get the first "studio" post for the current user
$studio = reset( $studio_query );

$studio_name = $studio->post_title;
$studio_option = get_post_meta( $studio->ID, 'wpcf-listing-option', true );

if ( in_array( $studio_option, array( 1, 2 ) ) ) {
$new_price = 180;
} elseif ( in_array( $studio_option, array( 3, 4 ) ) ) {
$new_price = 345;
} elseif ( in_array( $studio_option, array( 5, 6, 7 ) ) ) {
$new_price = 690;
}

$new_name = sprintf(
__( "Payment for 2020 Listing: %s - %s - Listing Option %s", "woocommerce" ),
$customer_id, $studio_name, $studio_option
);

// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Only for the defined product ID (or variation ID)
if ( in_array( $defined_product_id, [ $cart_item['product_id'], $cart_item['variation_id'] ] ) ) {
$cart_item['data']->set_price( $new_price );
$cart_item['data']->set_name( $new_name );
}
}
}

Overwrite cart item title and price from Toolset CRED form data in Woocommerce

Try the following instead as you are using the toolset plugin with a custom form, where you will use WC_Sessions to store the product ID and change the price:

add_filter( 'cred_commerce_add_product_to_cart', 'cred_commerce_add_product_to_cart', 20, 3 );
function cred_commerce_add_product_to_cart( $product_id, $form_id, $post_id ) {
if( $form_id == 55 && $post_id > 0 ) {
WC()->session->set( 'cp_id', $post_id );
}
return $product_id;
}

add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 30, 4 );
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ){
if ( WC()->session->get('cp_id') ) {
$cp_id = WC()->session->get('cp_id');
$product = wc_get_product($cp_id);
$cart_item_data['cp_price'] = $product->get_regular_price();
$cart_item_data['cp_title'] = $product->get_title();
}
return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_price', 30, 1 );
function custom_cart_items_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

// Iterating through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cat_item['cp_price']) && isset($cat_item['cp_title']) ){
$cart_item['data']->set_price( $cat_item['cp_price'] );
$cart_item['data']->set_name( $cat_item['cp_title'] );
}
}
}

Code goes in function.php file of your active child theme (or active theme). It should works.


Some related answers:

  • Set cart item price from a hidden input field custom price in Woocommerce 3
  • Custom cart item price based on user input in Woocommerce
  • Conditionally alter specific product price in Woocommerce
  • Change cart item prices in Woocommerce 3


Related Topics



Leave a reply



Submit