Fixes missing cart image by adjusting script enqueue and adding MutationObserver to handle dynamically rendered book wrappers
This commit is contained in:
parent
d4978a7122
commit
6fe38f323c
@ -1,23 +1,46 @@
|
|||||||
(function waitForBookWrappers(imageUrl) {
|
function addShopLinks(imageUrl) {
|
||||||
const interval = setInterval(() => {
|
document.querySelectorAll(".book-wrapper").forEach(wrapper => {
|
||||||
const bookWrappers = document.querySelectorAll(".book-wrapper");
|
if (wrapper.querySelector(".shop_link")) return;
|
||||||
if (bookWrappers.length === 0) return;
|
|
||||||
|
|
||||||
bookWrappers.forEach(wrapper => {
|
|
||||||
const coverLink = wrapper.querySelector(".sp__the-cover a");
|
const coverLink = wrapper.querySelector(".sp__the-cover a");
|
||||||
if (!coverLink || wrapper.querySelector(".shop_link")) return;
|
if (!coverLink) return;
|
||||||
|
|
||||||
const hrefParts = coverLink.getAttribute("href").split("/");
|
const href = coverLink.getAttribute("href");
|
||||||
const isbn = hrefParts[1];
|
const isbn = href.split("/")[1];
|
||||||
|
if (!isbn) return;
|
||||||
|
|
||||||
const shopLink = document.createElement("p");
|
const shopLink = document.createElement("p");
|
||||||
shopLink.innerHTML = `<a class="shop_link" href="https://cdcshoppingcart.uchicago.edu/Cart2/ChicagoBook?ISBN=${isbn}&PRESS=wvp" target="_blank">
|
shopLink.innerHTML = `
|
||||||
|
<a class="shop_link" href="https://cdcshoppingcart.uchicago.edu/Cart2/ChicagoBook?ISBN=${isbn}&PRESS=wvp" target="_blank">
|
||||||
<img src="${imageUrl}" alt="Shop Link">
|
<img src="${imageUrl}" alt="Shop Link">
|
||||||
</a>`;
|
</a>
|
||||||
|
`;
|
||||||
|
|
||||||
wrapper.appendChild(shopLink);
|
wrapper.appendChild(shopLink);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function waitForImageUrl(callback) {
|
||||||
|
if (window.SearchShopLinkData && window.SearchShopLinkData.imageUrl) {
|
||||||
|
callback(window.SearchShopLinkData.imageUrl);
|
||||||
|
} else {
|
||||||
|
setTimeout(() => waitForImageUrl(callback), 50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
waitForImageUrl(function(imageUrl) {
|
||||||
|
|
||||||
|
// Run once in case elements already exist
|
||||||
|
addShopLinks(imageUrl);
|
||||||
|
|
||||||
|
// Watch for dynamically added book wrappers
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
addShopLinks(imageUrl);
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(document.body, {
|
||||||
|
childList: true,
|
||||||
|
subtree: true
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
clearInterval(interval);
|
|
||||||
}, 100);
|
|
||||||
})(window.searchShopLinkImageUrl); // Pass URL dynamically
|
|
||||||
|
|||||||
21
plugin.php
21
plugin.php
@ -26,18 +26,31 @@ add_action('template_redirect', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function enqueue_shop_links_script() {
|
function enqueue_shop_links_script() {
|
||||||
if (is_singular()) {
|
if (is_singular()) {
|
||||||
wp_enqueue_script('search_shop_link',plugins_url('/js/search_shop_link.js', __FILE__));
|
|
||||||
wp_add_inline_script( 'search_shop_link', 'window.searchShopLinkImageUrl = "' . plugins_url('assets/cart_add.png', __FILE__) . '";' );
|
|
||||||
|
|
||||||
|
// 1. Enqueue your JS file
|
||||||
|
wp_enqueue_script(
|
||||||
|
'search_shop_link',
|
||||||
|
plugins_url('js/search_shop_link.js', __FILE__),
|
||||||
|
[],
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. Localize data for that script
|
||||||
|
wp_localize_script(
|
||||||
|
'search_shop_link',
|
||||||
|
'SearchShopLinkData',
|
||||||
|
[
|
||||||
|
'imageUrl' => plugins_url('assets/cart_add.png', __FILE__)
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
add_action('wp_enqueue_scripts', 'enqueue_shop_links_script');
|
add_action('wp_enqueue_scripts', 'enqueue_shop_links_script');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function br_replace_book_wrapper($html) {
|
function br_replace_book_wrapper($html) {
|
||||||
if (strpos($html, 'product-details') === false) {
|
if (strpos($html, 'product-details') === false) {
|
||||||
return $html;
|
return $html;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user