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:
function vendor_defined_taxonomy() {
return 'Materijal';
}
add_action( 'woocommerce_product_meta_end', 'display_product_vendors', 10 );
function display_product_vendors() {
$taxonomy = vendor_defined_taxonomy();
$term_ids = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' =>
'ids') );
if( sizeof($term_ids) > 0 ){
echo '<span class="posted_in vendors"></span>';
}
}
add_filter( 'woocommerce_available_variation', 'selected_variation_vendor_value', 10, 3 );
function selected_variation_vendor_value( $data, $product, $variation ) {
$taxonomy = vendor_defined_taxonomy();
if( isset($data['attributes']['attribute_'.$taxonomy]) )
$term = get_term_by( 'slug', $data['attributes']['attribute_'.$taxonomy], $taxonomy );
if( isset($term) && is_a($term, 'WP_Term' ) )
$data['variation_description'] .= '<input type="hidden" name="vendor- hidden" id="vendor-hidden" value="'.$term->name.'">';
return $data;
}
add_action('woocommerce_before_add_to_cart_button', 'custom_product_jquery_script');
function custom_product_jquery_script() {
global $product;
$taxonomy = vendor_defined_taxonomy();
$terms_string = $product->get_attribute($taxonomy);
if( ! empty($terms_string) ) :
?>
<script type="text/javascript">
jQuery(function($) {
var form = 'form.variations_form',
selected = 'input[name="variation_id"]',
vendorVal = 'input#vendor-hidden',
vendorTarget = 'span.vendors',
vendorHtml = $(vendorTarget).text(),
vendorLabel = '';
// On variation select
$(form).on( 'blur', 'select', function() {
if($(selected).val() != ''){
$(vendorTarget).text("");
if($(vendorVal).val() == 'Zlato'){
//$(vendorTarget).text(vendorLabel+'
'+$(vendorVal).val());
$(vendorTarget).text("here is your text");
}
}
});
});
</script>
<?php
endif;
}
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?