Hi,

I'm a complete novice to PHP but have been using Wordpress for many years which has included adding various PHP codes for templates which I usually source from either the wordpress.org website or from forums. A few months back I set up some code on my site which I'll explain more below; it worked fine but since updates it no longer functions as it should and I'm pretty sure it's a basic fix needed but as I've not been able to get my head around the code I'm at a bit of a loss on how to fix it; which has led me to here.

If someone could help me with the below, and if you specialise in PHP please also leave me your contact details as I have on going work on Wordpress and would gladly pass anything I can your way.

The problem:

I'm using the latest version of Wordpress with the plugin WooCommerce for the shop. The code I added is to the single-product.php template. All the code done was display the price, the VAT and the price + VAT, so basically 3 lines showing a breakdown of the price. Rather than just the total which displays by default. I think WooCommerce had an update which means I need to adjust my code but I have no idea what to change. The code is no longer live on my website but if you need to see a live example to understand what's going on please let me know and I'll set it up again so you can see it, please find the code below I look forward to your replies 😃

if ( $product->get_price_html() ) :
// Get the prices
$price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
// Display the prices
?>
<span class="price price-excl"><?php echo wc_price( $price_excl_tax ); ?></span><br>
<span class="price tax-price">+ VAT = <?php echo wc_price( $tax_amount ); ?></span><br>
<span class="price price-incl">Total = <?php echo wc_price( $price_incl_tax ); ?></span>

    My wild guess is that maybe the plugin was updated, and it either overwrote the file that you modified, or else the plugin's structure changed such that it doesn't even call that file any more. But as someone who hasn't really touched WordPress in several years and never used WooCommerce, maybe someone smarter than I will come along soon with more specific suggestions. 🙂

      Thanks for your reply NogDog, thats what's happened but i'm pretty certain it's only the code which needs a slight adjustment as the template is still used; and as usual it was overwrote when updating but i re-added it then noticed the code no longer worked as it should.. i think something like "$price_excl_tax" may have changed; but i'm not to sure what this is and couldn't work anything out from the changelog. I keep my plugins up to date so it's an update within the last 2 months which has caused the issues.

      P.s WooCommerce uses template files to display the data so in this case "single-product.php" is used to display the information on product pages, i used the code above to change how the price is displayed (hope that helps)

        I might try something like this at first, just to see if you're getting into that code:

        echo "<p>About to hit the if</p>";
        if ( $product->get_price_html() ) :
        echo "<p>Made it into the if</p>";
        

        (Or error_log() instead of echo if you prefer?)
        If neither shows up, then you at least know that you need to figure out why it's not getting there at all, whereas if only the first debug paragraph show, then the likely suspect is the get_price_html() method.

        PS: might be useful to add this while debugging:

        <?php
        ini_set('display_errors', true); // change to false or remove when you go live
        error_reporting(E_ALL);
        // rest of code...
        

        NogDog Thanks again for you reply, the code displays ok it's just not working as it should (Prices display as 0.00 and it also displays part of the code frontend) Debugging is beyond my PHP knowledge; if i set up a live draft site to replicate the issue would you be interested in taking a look?

          Well i'll be damned, just set up a test site and the code works fine so must be something conflicting on the actual website; is there any advice you can offer on how to approach troubleshooting my site?

            All fixed, not even a PHP issue #WordpressProblems

              Just an aside here - you shouldn't modify the plugin files at all. WooCommerce templates are nothing but hooks, so you can modify the output via your functions.php or another custom plugin file, or duplicate the file into a sub-directory of your theme as described in the manual and make your changes there. That way any changes you make aren't overwritten every time you update the plugin. Which, by the way, is quite often.

                Thanks for your input @maxxd i had actually set it up in a sub directory as you mentioned but occasionally when updating i still have to re-upload the files; curious how would i go about adding it to the functions.php The file i edit is single-product.php how do you target the template from the functions file?

                This is is the custom code used:

                if ( $product->get_price_html() ) :
                // Get the prices
                $price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
                $price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
                $tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
                // Display the prices
                ?>
                <span class="price price-excl"><?php echo wc_price( $price_excl_tax ); ?></span><br>
                <span class="price tax-price">+ VAT = <?php echo wc_price( $tax_amount ); ?></span><br>
                <span class="price price-incl">Total = <?php echo wc_price( $price_incl_tax ); ?></span>

                JackieChan i had actually set it up in a sub directory as you mentioned

                In which case you're fine - I'd be interested to know why you still have to upload the files on occasion, unless it's due to updates that WC made that caused the versioning to fall out of sync.

                As for using the functions.php file, unless they've changed something drastically in the past 2 or 3 months, WC templates are far more hooks than HTML. You can use those hooks just like any other in the system to modify what the template displays.

                23 days later

                maxxd Thanks i think initially i set it up wrong updates fine now!

                  11 days later

                  With respect to utilizing the functions.php record, except if they've changed something radically in the previous 50 TO 70 days, WC layouts are definitely a bigger number of snares than HTML. You can utilize those snares simply like some other in the framework to change what the layout shows.

                  Thomas4 Thanks for your input, how do you go about finding the names of those snares?

                    Write a Reply...