0

I'm currently working with a Wordpress plugin. I have amended one of the plugin files by adding some simple HTML code and a PHP variable associated with the plugin. Essentially duplicating a piece of code already in the file so I can repurpose the code using simple CSS.

(Essentially, the plugin dynamically displays the grand total of a product as different features are added. I just needed to duplicate this and display it as a fixed banner, so nothing in the plugin file I have added is new or changes the way the plugin functions.)

I'd like to be able to append this code using javascript so that when I update the plugin, my changes won't be lost.

This is what I have so far but I've had no success so far

function insertAfter(referenceNode, newNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

var el = document.createElement("div");
el.innerHTML = "<div class="gt-banner-message">
    <div class="total-wrap">
            <span><?php _e('Grand total: ','advanced-product-fields-for-woocommerce'); ?></span>
            <span class="wapf-grand-total price amount"></span>
        </div>
    </div> ";
var div = document.getElementsByClassName("wapf-product-totals");
insertAfter(div, el);

Can someone let me know where I maybe going wrong here. Any advice much appreciated.

Adam
  • 303
  • 4
  • 16
  • `el.innerHTML = "
    – CBroe Nov 09 '21 at 15:46
  • You need to fix the quotes issue. You cannot call PHP to run from a JS div ...:) doesn't work that way ... what you can do is call that data from before the JS runs... to a PHP variable and display it with JS - you can run this PHP function and return it to a PHP var like: $productFields and in JS var DatafromPHP = '' -- this applies if the output of the function is this a text string. – Shlomtzion Nov 09 '21 at 16:02
  • @Shlomtzion Thanks for the reply. So are you saying that the php content inside the span in my code above would need to be declared even before the js function? – Adam Nov 09 '21 at 18:54
  • Yes, get the data on PHP that runs before the JS and use it with JS. – Shlomtzion Nov 09 '21 at 19:37

0 Answers0