Search code examples
phpwordpresswoocommercerequiredvariable-product

Add a red asterisk at the end of all attributes labels in Woocommerce variable products


How do I add a red asterisk to the end of all attributes labels in Woocommerce? It seems like it should be simple, but I'm having a hard time with figuring it out. Wouldn't it be in the functions.php file?

The required attributes are actually working because the Add to Cart button is grayed out until an option is selected. However, apparently my customers don't realize this because they are telling me my Add to Cart button doesn't work. So, if I could draw their attention to the variations dropdown menu maybe they would realize that an option must be chosen before they can add the item to their cart.


Solution

  • To add a red asterisk at then end of product attribute labels on single variable products, just like in required checkout fields, you can use the following very simple hooked function:

    add_filter( 'woocommerce_attribute_label', 'filter_single_variable_product_attribute_label', 10, 3 );
    function filter_single_variable_product_attribute_label( $label, $name, $product ) {
        if ( is_product() ){
            $label .= '&nbsp;<abbr class="required" title="required" style="color:#FF3333;">*</abbr>';
        }
        return $label;
    }
    

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