Ecommerce Shopify WordPress Discussion

Change woocommerce product name/title from a product custom field

I would like to overwrite the name/title of single products on the category and the product page also, with an existing 'customname1' custom field. The change should be applied whenever the 'customname1' field is present. I have found a modification for the checkout and the cart page as the following. I would like to have the same effect on the Category and the Product page. function woocommerce_update_product_title($title, $cart_item){ if( is_checkout() || is_cart() ) : //Check Checkout or Cart Page $customname1= get_post_meta($cart_item['product_id'], 'customname1',true); return !empty( $customname1) ? $customname1: $title; endif; return $title; } add_filter('woocommerce_cart_item_name', 'woocommerce_update_product_title', 10, 2);
Instead, you should use the following code replacement, that will work for everywhere, replacing your current provided code. The following hooked functions will change or filter the product title or name, based on your custom field value, when any code is using get_title() or get_name() methods: add_filter( 'woocommerce_product_title', 'custom_product_title', 10, 2 ); function custom_product_title( $title, $product ) { if ( $custom_title = $product->get_meta('customname1') ) { return $custom_title; } return $title; } add_filter( 'woocommerce_product_get_name', 'custom_product_name', 10, 2 ); function custom_product_name( $name, $product ) { if ( $custom_name = $product->get_meta('customname1') ) { return $custom_name; } return $name; } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

February 13, 2024

Certainly! To achieve the same effect on the category and product pages, you can use these filters specific to those pages: For the product page, you can use the woocommerce_single_product_summary hook. for the category page, you can use the woocommerce_before_shop_loop_item_title hook. Here's the modified code: function woocommerce_update_product_title( $title, $cart_item = null ) { global $post; // Check if on the product page if ( is_product() ) { $customname1 = get_post_meta( $post->ID, 'customname1', true ); return ! empty( $customname1 ) ? $customname1 : $title; } // Check if on the category page if ( is_shop() || is_product_category() ) { $customname1 = get_post_meta( get_the_ID(), 'customname1', true ); return ! empty( $customname1 ) ? $customname1 : $title; } // For other pages (fallback) return $title; } // Hook for product page add_filter( 'the_title', 'woocommerce_update_product_title', 10, 2 ); // Hook for category page add_filter( 'woocommerce_before_shop_loop_item_title', 'woocommerce_update_product_title', 10, 2 ); This code checks if you are on a product page using is_product() and updates the title accordingly. If you are on the category page or the shop page, it checks for the presence of the customname1 custom field and updates the title if it exists. Make sure to adjust the hooks and conditions based on your specific requirements and theme structure.

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.