Skip to content
Advertisement

Change WooCommerce variable product title based on variations

I’ve coded in a custom select box to a group of my products that changes the price based on the user’s selection. It works well, but now I need to change the product title too based on their selection.

Basically if option 1 is chosen, the product name stays the same. But is option 2 is chosen, I need to add “-RZ” to the end of the product title.

I’m not sure if I can do this in the ‘woocommerce_before_calculate_totals’ hook where I altered the prices, but if anyone knows the hook I should use and the code to access the current product’s title via PHP that would be great.

Here is the code that alters the price if it’s helpful:

JavaScript

I know may have to get the name and store it in a SESSION variable to use later if the hook to do this is on the cart, checkout, or order page rather than the single-product page.

Advertisement

Answer

Yes this is possible in the same hook. You can manipulate the product title with class WC_Product get_name() and set_name() methods. But as for the price, you should set and get a custom cart field value to make it (just as $additionalPrice = $value['core'];).

See here a simple related answer: Changing WooCommerce cart item names

So you could have something like (just a fake example):

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement