Add HTML Before Add to Cart, Show if it has certain attribute, Variable Product

Adding custom html if the current product has a Width attribute for variable product.

This code checks for Width but can be changed to any attribute.



// Custom HTML Before Add to Cart Form 
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_html_before_add_to_cart_form' );

function add_custom_html_before_add_to_cart_form() {
    // Get product attributes from the meta
    $product_attributes = maybe_unserialize(get_post_meta(get_the_ID(), '_product_attributes', true)); 
    
    // Check if 'Width' attribute exists in the product attributes
    if ( isset($product_attributes['Width']) ) {
        ?>
        
        <div id="brxe-cdeirb"  >
            Hello world
        </div>
        
        <?php 
    }
}

Leave the first comment