CharlieEtienne / sold-out-badge-for-woocommerce

Display a "Sold out" badge on out of stock WooCommerce products
https://wordpress.org/plugins/sold-out-badge-for-woocommerce/
GNU General Public License v2.0
2 stars 3 forks source link

Can I use HTML in the Badge text field in the admin panel? #18

Closed muhyal closed 2 years ago

muhyal commented 2 years ago

Hi,

Can I use HTML in the Badge text field in the admin panel?

If I use HTML it shows the entered value as plain text:

2022-09-07_20-07-32

For example, for a badge that looks like this:

2022-09-07_20-06-16

I want to set it like this:

2022-09-07_20-06-22

I did some research and found a document like this, but I couldn't get a successful result: https://carbonfields.net/docs/fields-html/

Thank you for your support.

CharlieEtienne commented 2 years ago

Hi @muhyal,

In fact this is not a limitation of carbon fields or my plugin, but a limitation from WooCommerce. The hook used to display the badge is escaping html.

So the workarounds would be to override the products pages in your theme, or to replace WC hook.

muhyal commented 2 years ago

Thanks, @CharlieEtienne,

Do you have any guidance on how to do it?

CharlieEtienne commented 2 years ago

Wait, I'm not sure of what I told in my previous message... Let me check.

CharlieEtienne commented 2 years ago

I was completely wrong, sorry. I was thinking to another plugin. The HTML is actually escaped in my plugin, in wp-content/plugins/sold-out-badge-for-woocommerce/classes/Settings.php on line 125.

Could you try this code (in your theme's functions.php for example) and let me know if it works?

global $post, $product;
add_filter('wcsob_soldout', function($content) {
    return htmlspecialchars_decode($content, ENT_QUOTES);
}, $post, $product);
muhyal commented 2 years ago

I tested it but this time the theme is broken.

It looks like this:

2022-09-08_11-07-19

CharlieEtienne commented 2 years ago

Yes, I forgot to specify the number of arguments. This is the right function to use:

global $post, $product;
add_filter('wcsob_soldout', function ($content, $post, $product) {
    return htmlspecialchars_decode($content, ENT_QUOTES);
}, 10, 3);
muhyal commented 2 years ago

You've probably heard this a lot in your life @CharlieEtienne . "You are a great person" :) Thanks for your support.

Looks just like we imagined:

2022-09-08_15-56-31

CharlieEtienne commented 2 years ago

You're welcome!