Code-Snippets-Functions/Execute a function on a child site/WooCommerce/display-the-selected-attribute-option-name-near-the-label.txt

28 lines
962 B
Text

add_action('woocommerce_before_add_to_cart_form', 'display_specific_selected_attribute_term_name', 25);
function display_specific_selected_attribute_term_name() {
global $product;
if ( !$product->is_type('variable') ) {
return; // Exit if it's not a variable product
}
$taxonomy = 'pa_spieki'; // <== HERE set the attribute taxonomy
wc_enqueue_js("const selAttr = 'select#{$taxonomy}',
selAttrOption = selAttr+' option:selected',
selLabel = 'label[for={$taxonomy}]',
selSpan = 'span.for-{$taxonomy}';
$(selLabel).after(' <span class=\"for-{$taxonomy}\"></span>');
function displayAttrSelectedOptionText(){
const textValue = $(selAttr).val() != '' ? $(selAttrOption).text() : '';
$(selSpan).text(textValue);
}
displayAttrSelectedOptionText();
$('form.cart').on('change', 'select#{$taxonomy}', function() {
displayAttrSelectedOptionText();
})");
}