[{"data":1,"prerenderedAt":259},["ShallowReactive",2],{"blog-\u002Fblog\u002Fcustomizing-woocommerce-sale-badge-based-on-products-category":3},{"id":4,"title":5,"body":6,"date":247,"description":248,"extension":249,"image":250,"meta":251,"navigation":252,"path":253,"seo":254,"stem":255,"tags":256,"__hash__":258},"blog\u002Fblog\u002Fcustomizing-woocommerce-sale-badge-based-on-products-category.md","Customizing Woocommerce Sale Badge based on Product's Category",{"type":7,"value":8,"toc":244},"minimark",[9,21,30,35,38,228,240],[10,11,12,13,20],"p",{},"Lately, I was working on an e-commerce shop built with Woocommerce for ease of use, as ",[14,15,19],"a",{"href":16,"rel":17},"https:\u002F\u002Fwoocommerce.com\u002Fmobile\u002F",[18],"nofollow","they've got also a great-looking app"," for managing your store on the way. There is a great variety of Plugins you can use on your Woocommerce setup.",[10,22,23,24,29],{},"The most needed feature of an e-commerce store, especially for targeting ads through the web, is a coupon\u002Fdiscount manager. Ended up using ",[14,25,28],{"href":26,"rel":27},"https:\u002F\u002Fwordpress.org\u002Fplugins\u002Felex-woocommerce-dynamic-pricing-and-discounts\u002F",[18],"ELEX WooCommerce Dynamic Pricing and Discounts"," as I needed a global discount, based on the Product's category. The final result was pretty decent, but it would be great to fully customize based on the product's category.",[31,32,34],"h2",{"id":33},"customizing-based-on-category-id","Customizing based on Category ID",[10,36,37],{},"As there isn't any way to do it from the Dashboard, we should add a small snippet in functions.php",[39,40,45],"pre",{"className":41,"code":42,"language":43,"meta":44,"style":44},"language-php shiki shiki-themes github-dark","\u002F\u002F Sale badge\nadd_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);\nfunction woocommerce_custom_sale_text($text, $post, $_product)\n{\n    $terms = get_the_terms( $post->ID, 'product_cat' );\n    foreach ($terms  as $term  ) {\n        $product_cat_id = $term->term_id;\n        if ($product_cat_id == 68) {\n            return '\u003Cspan class=\"onsale offer\">OFFER\u003C\u002Fspan>';\n        }\n    }\n    return '\u003Cspan class=\"onsale\">DISCOUNT\u003C\u002Fspan>';\n}\n","php","",[46,47,48,57,92,105,111,138,153,169,187,199,205,211,222],"code",{"__ignoreMap":44},[49,50,53],"span",{"class":51,"line":52},"line",1,[49,54,56],{"class":55},"sAwPA","\u002F\u002F Sale badge\n",[49,58,60,64,68,72,75,78,80,84,86,89],{"class":51,"line":59},2,[49,61,63],{"class":62},"svObZ","add_filter",[49,65,67],{"class":66},"s95oV","(",[49,69,71],{"class":70},"sU2Wk","'woocommerce_sale_flash'",[49,73,74],{"class":66},", ",[49,76,77],{"class":70},"'woocommerce_custom_sale_text'",[49,79,74],{"class":66},[49,81,83],{"class":82},"sDLfK","10",[49,85,74],{"class":66},[49,87,88],{"class":82},"3",[49,90,91],{"class":66},");\n",[49,93,95,99,102],{"class":51,"line":94},3,[49,96,98],{"class":97},"snl16","function",[49,100,101],{"class":62}," woocommerce_custom_sale_text",[49,103,104],{"class":66},"($text, $post, $_product)\n",[49,106,108],{"class":51,"line":107},4,[49,109,110],{"class":66},"{\n",[49,112,114,117,120,123,126,129,132,135],{"class":51,"line":113},5,[49,115,116],{"class":66},"    $terms ",[49,118,119],{"class":97},"=",[49,121,122],{"class":62}," get_the_terms",[49,124,125],{"class":66},"( $post",[49,127,128],{"class":97},"->",[49,130,131],{"class":66},"ID, ",[49,133,134],{"class":70},"'product_cat'",[49,136,137],{"class":66}," );\n",[49,139,141,144,147,150],{"class":51,"line":140},6,[49,142,143],{"class":97},"    foreach",[49,145,146],{"class":66}," ($terms  ",[49,148,149],{"class":97},"as",[49,151,152],{"class":66}," $term  ) {\n",[49,154,156,159,161,164,166],{"class":51,"line":155},7,[49,157,158],{"class":66},"        $product_cat_id ",[49,160,119],{"class":97},[49,162,163],{"class":66}," $term",[49,165,128],{"class":97},[49,167,168],{"class":66},"term_id;\n",[49,170,172,175,178,181,184],{"class":51,"line":171},8,[49,173,174],{"class":97},"        if",[49,176,177],{"class":66}," ($product_cat_id ",[49,179,180],{"class":97},"==",[49,182,183],{"class":82}," 68",[49,185,186],{"class":66},") {\n",[49,188,190,193,196],{"class":51,"line":189},9,[49,191,192],{"class":97},"            return",[49,194,195],{"class":70}," '\u003Cspan class=\"onsale offer\">OFFER\u003C\u002Fspan>'",[49,197,198],{"class":66},";\n",[49,200,202],{"class":51,"line":201},10,[49,203,204],{"class":66},"        }\n",[49,206,208],{"class":51,"line":207},11,[49,209,210],{"class":66},"    }\n",[49,212,214,217,220],{"class":51,"line":213},12,[49,215,216],{"class":97},"    return",[49,218,219],{"class":70}," '\u003Cspan class=\"onsale\">DISCOUNT\u003C\u002Fspan>'",[49,221,198],{"class":66},[49,223,225],{"class":51,"line":224},13,[49,226,227],{"class":66},"}\n",[10,229,230,231,235,236,239],{},"On the above snippet ",[232,233,234],"strong",{},"68"," is the ",[232,237,238],{},"Category ID"," and also I added an extra CSS class called \"offer\". In this way you can add as much conditions as you like, but also make the color of event fonts differ based on the CSS rules you'll apply.",[241,242,243],"style",{},"html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":44,"searchDepth":59,"depth":59,"links":245},[246],{"id":33,"depth":59,"text":34},"2021-01-30","Lately, I was working on an e-commerce shop built with Woocommerce for ease of use, as [they've got also a great-looking app](https:\u002F\u002Fwoocommerce.com\u002Fmobile\u002F) f","md",null,{},true,"\u002Fblog\u002Fcustomizing-woocommerce-sale-badge-based-on-products-category",{"title":5,"description":248},"blog\u002Fcustomizing-woocommerce-sale-badge-based-on-products-category",[257],"quickfix","YiEZFwC_54pEU1YBCKKQeQSOhypwemB4Dl6KDKpntk8",1785393036278]