Hi what I'm trying to do is run a campaign where all products on the site will have a sale banner. Some products will already have a sale banner, but for the rest of the products that don't then i want to add a banner to these. I've been able to do this, but the issue i'm having is, if a product is Out of Stock then i don't want the sale banner to be added to these products.
I've created a snippet trying to show what i've got so far.
- Product 1 is a product that already has a sale banner.
- Product 2 is a product that doesn't have a sale banner
- Product 3 is a product that doesn't have a sale banner and is out of stock
Product 1 has a sale banner so nothing needs to be done to this. For Product 2 i want the sale banner to be added & for Product 3 i don't want a sale banner to be added as it is out of stock.
Any help on this would be much appreciated.
$(".saleWrapper").each(function(index){
if ($(this).children('.promoText').length === 0){
$(this).append("<span class='promoText'>I'm ALSO on Sale</span>");
}
})
.product-wrapper {
display: inline-block;
border: 1px solid black;
}
.promoText {background: red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-wrapper">
<p>Product 1</p>
<div class="saleWrapper">
<span class="promoText">I'm on Sale</span>
</div>
</div>
<div class="product-wrapper">
<p>Product 2</p>
<div class="saleWrapper"></div>
</div>
<div class="product-wrapper">
<p>Product 3</p>
<div class="saleWrapper"></div>
<div class="productOutOfStock">
<p class="outOfStock">I'm Out Of Stock</p>
</div>
</div>