0

I want to add a div with a class inside of a existing element in WordPress/Woocommerce. I want to make the sale price more pretty :) Let's start with what I have now: enter image description here

Code:

<ins>
    <span class="woocommerce-Price-amount amount">
        <bdi>
            <span class="woocommerce-Price-currencySymbol">$</span>200.00
        </bdi>
    </span>
</ins>

And this is what I am trying to achieve: enter image description here

Code:

<ins>
    <span class="woocommerce-Price-amount amount">
        <div class="vh-badge-ears"></div>
        <bdi>
            <span class="woocommerce-Price-currencySymbol">$</span>200.00
        </bdi>
    </span>
</ins>

the closest I have come to is adding this code to my functions.php:

function bd_sale_price_html( $price, $product ) {
  if ( $product->is_on_sale() ) :
    $return_string = str_replace( '<ins><span class="woocommerce-Price-amount amount">', '<ins><span class="woocommerce-Price-amount amount"><div class="vh-badge-ears"></div>', $price);
    return $return_string;
  else :
    return $price;
  endif;
}
add_filter( 'woocommerce_get_price_html', 'bd_sale_price_html', 100, 2 );

But by using this code it looks like this: enter image description here

I hope someone here could help me out.

Kind regards!

7
  • div is block element and should not be inside span. Validate your HTML with w3 validator. Commented Jan 4, 2023 at 17:24
  • Can you just use ::before for this instead? Commented Jan 4, 2023 at 17:37
  • @ChrisHaas Hey sorry for the nooby question that I'm about to ask, but how would I use ::before? Like this? : .price > ins > .amount { position: relative; background-color: #28a745; color: #ffffff; padding-left: 5px; padding-right: 5px; border-radius: 5px; } .price > ins > .amount::before { position: absolute; background-image: url(./ears.svg); background-size: contain; background-repeat: no-repeat; background-position: center; width: 100%; height: 100%; top: -80%; } Commented Jan 4, 2023 at 17:53
  • I don't know the specifics of what you are doing, but ::before will give you one free virtual DOM element that you can do things with, just make sure to give it content of some sort, which can just be empty: codepen.io/cjhaas/pen/yLqaqbZ Commented Jan 4, 2023 at 18:00
  • @ChrisHaas It did seem like a solution but unfortunately the CSS content property doesn't allow HTML markup... I tried: .amount > bdi::before { content: '<div class="vh-badge-ears"></div>'; } Commented Jan 4, 2023 at 18:09

1 Answer 1

2

Full credits to @ChrisHaas

Instead of adding a div inside of the element, he recommended using ::before in css.

So the final code that helped me achieve what I wanted is:

.price > ins > .amount {
    position: relative;
    background-color: #28a745;
    color: #ffffff;
    padding-left: 5px;
    padding-right: 5px;
    border-radius: 5px;
}
.price > ins > .amount > bdi::before {
    content: "";
    position: absolute;
    background-image: url(./ears.svg);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 100%;
    height: 100%;
    top: -80%;
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.