Skip to content
Advertisement

Set and display a custom product variation value dynamically on WooCommerce single products

I have a WooCommerce jewelry site. On single variable product page I have variations and I added a text box (text box is for the customer to write something that will be printed on the jewelry).

So I expect the page to work as follows: If the customer selects a particular product variation and starts typing in the textbox price of the product should change depending on number of letters (NOTE: this should only work if one specific variation is selected) on every other product price will be fixed.

I did manage to do the part for the textbox price update.

The issue I am having here is with the selection of specific product variation.

Based on Display the product attribute term for the selected variation in Woocommerce answer, I tried to solve this problem with the following code attempt:

JavaScript

But I can’t make it work to print the “Matrijal” product attribute selected name value under product meta section.

Product attribute name is “Matrijal” (and slug “mats”) … and for example a term name is “Zlato” (and slug “zlato”).

Any help?

Advertisement

Answer

You are making things more complicated that they should be…

Here is a lightweight and effective way to add specific selected variation custom value (from specific product attribute) to an additional html ” after single product meta.

You just need to define the correct product attribute taxonomy in the first function.

The code:

JavaScript

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

Tested and works on last WooCommerce version (4.9.2) on a variable product with a defined product attribute (for variations) taxonomy.

Remember that product attribute taxonomy always start with “pa_” + product attribute slug.

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