mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
https://stackoverflow.com/questions/78076107/display-the-selected-attribute-option-name-near-the-label-in-woocommerce-variabl
28 lines
962 B
Text
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();
|
|
})");
|
|
}
|