Ecommerce Shopify WordPress Discussion

Change specific product cart item name in WooCommerce

I am using the following code to change product names on checkout page, but I need to do it for only one product. I am not able to figure out how to target a specific product ID: 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; } } The code changes ALL the product names on WooCommerce checkout page to the new product name. But I need to do it for a specific product only (product with ID: 134443).
Your code can be simplified, targeting a specific product to change its name. Try: add_action( 'woocommerce_before_calculate_totals', 'change_specific_cart_item_name' ); function change_specific_cart_item_name( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $targeted_id = 134443; // HERE set the targeted product ID $new_name = "The new product name"; // HERE set the desired new product name // Loop through cart items foreach ( $cart->get_cart() as $cart_item ) { if ( $cart_item['product_id'] == $targeted_id ) { $cart_item['data']->set_name($new_name); } } } Code goes in functions.php file of your child theme (or in a plugin). It should work.

February 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.