0

I am trying to print a message (link) through PHP if a div is present on a page. Basically the div is present on some product pages on an ecommerce website, while it is not present on other product pages. The ecommerce is a Woocommerce and that's why I am using Woo hooks in the code.

Basically I get nothing in return (even when visiting pages where the div is present). What am I missing?

add_filter('woocommerce_before_add_to_cart_button', 'size_guide_display', 15);
function size_guide_display() {
    $sizeguide = $html->find("div#tab-SizeChart_tab");
    if ($sizeguide) :
        echo '<a href="#tab-SizeChart_tab" title="Storleksguide" class="size_guide_link">Storleksguide</a>';
    endif;
}

I'll be very happy for any reply :-) Pretty novice with PHP.

Thanks!

Edvin Uddfalk
  • 381
  • 3
  • 13

1 Answers1

1

Due to the information you've provided I guess using JavaScript on the client-side would be your way to go.

document.addEventListener("DOMContentLoaded", () => {
    const sizeChartTabEle = document.querySelector("div#tab-SizeChart_tab")
    const storleksGuideLinkEle = document.createElement("a")
    storleksGuideLinkEle.title = "Storleksguide"
    storleksGuideLinkEle.href = "#tab-SizeChart_tab"
    storleksGuideLinkEle.classList.add("size_guide_link")
    storleksGuideLinkEle.innerText = "Storleksguide"
    sizeChartTabEle?.append(storleksGuideLinkEle)
})

This code creates an event listener DOMContentLoaded (see this StackOverflow answer to learn more about it, speaking very basic - it waits for your contents being loaded) and executes the part in the curly brackets.

Your div#tab-SizeChart_tab is queried and afterwards appended by a newly created a element storleksGuideLinkEle.

If you make sure this code is only executed on pages that you need it, this should do the job.

WebDevPassion
  • 422
  • 1
  • 4
  • 12