Adding html to WooCommerce Store Notice using filters

与世无争的帅哥 提交于 2021-02-08 09:27:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/52186414/adding-html-to-woocommerce-store-notice-using-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!