phpwordpresswoocommerceadd-filter

Adding html to WooCommerce Store Notice using filters


I am trying to add a div wrapper around my WooCommerce Store Notice using a filter. And I also want to replace the dismiss link with a close icon.

This is what I have so far and it doesn't really work how I want it to;

add_filter('woocommerce_demo_store', 'demo_store_filter');

function demo_store_filter($text){
    return str_replace( '<p class="woocommerce-store-notice demo_store">', '<div class="hello"><p class="woocommerce-store-notice demo_store"></p></div>', $text);
}

Here is the default html for the notice;

<p class="woocommerce-store-notice demo_store">Enter the code <strong>'TLFS5V'</strong> to received £10 off your order when you spend over £100 <a href="#" class="woocommerce-store-notice__dismiss-link">Dismiss</a></p>

Here is what I want;

<div class="container-fluid"><div class="container"><p class="woocommerce-store-notice demo_store">Enter the code <strong>'TLFS5V'</strong> to received £10 off your order when you spend over £100 <a href="#" class="woocommerce-store-notice__dismiss-link">(close svg icon here)</a></p></div></div>

Is this possible with filters? Or should I do this by creating a new function?


Solution

  • you can do it this way :

    add_filter('woocommerce_demo_store', 'demo_store_filter', 10, 1);
    
    function demo_store_filter($text)
    {
    
        $text = str_replace(array('<p class="woocommerce-store-notice demo_store">', '</p>', 'Dismiss'), array('<div class="hello"><p class="woocommerce-store-notice demo_store">', '</p></div>', '(close svg icon here)'), $text);
    
        return $text;
    }
    

    output:

    <div class="hello"><p class="woocommerce-store-notice demo_store">as <a href="#" class="woocommerce-store-notice__dismiss-link">(close svg icon here)</a></p></div>
    

    tested and working.