Skip to content
Advertisement

WooCommerce – Can’t add a inside the return

I’m using the following code to change the add to cart button text in WooCommerce.

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
    // Variable products
    if( $product->is_type('variable') ) {
        // shop and archives
        if( ! is_product() ){

            $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
            return strip_tags( $product_price ) . ' — ' . $button_text;
        }
        // Single product pages
        else {
            $variations_data =[]; // Initializing

        // Loop through variations data
        foreach($product->get_available_variations() as $variation ) {
            // Set for each variation ID the corresponding price in the data array (to be used in jQuery)
            $variations_data[$variation['variation_id']] = $variation['display_price'];
        }
        ?>
        <script>
        jQuery(function($) {
            var jsonData = <?php echo json_encode($variations_data); ?>,
                inputVID = 'input.variation_id';

            $('input').change( function(){
                if( '' != $(inputVID).val() ) {
                    var vid      = $(inputVID).val(), // VARIATION ID
                       vprice   = ''; // Initilizing

                    // Loop through variation IDs / Prices pairs
                    $.each( jsonData, function( index, price ) {
                        if( index == $(inputVID).val() ) {
                            vprice = Math.round(price); // The right variation price
                        }
                    });
                    // Change price dynamically when changing options
                    $( "button.single_add_to_cart_button.button.alt span" ).remove();
                    $(".single_add_to_cart_button").prepend("<span>" + "$" + vprice + " — " + "</span>");
                }
            });
        });
        </script><?php
            return $button_text;
        }
    }
    // All other product types
    else {
        $product_price = wc_price( wc_get_price_to_display( $product ) );
        return strip_tags( $product_price ) . ' — ' . $button_text;
    }
}

The issue is in this bit of code here:

$product_price = wc_price( wc_get_price_to_display( $product ) );
return strip_tags( $product_price ) . ' — ' . $button_text;

I’m trying to add a <span> inside the outputted HTML but it’s not including the < and >:

<a href="?add-to-cart=148">&lt;span&gt;$25.00 —  &lt;/span&gt;Add to cart</a>

I’ve tried for hours to try and figure out why.

Any help would be greatly appreciated.

Thanks

Advertisement

Answer

This is caused by strip_tags function, see here for its usage

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement