Dynamically Insert Product ID from URL into YITH Wishlist Shortcode
-
Hello everyone,
I’m trying to dynamically insert a product ID from the page URL into the YITH WooCommerce Wishlist shortcode.
Specifically, if my URL looks like this:
https://website.com/product/product_name?id=24746
I want the shortcode to be:[yith_wcwl_add_to_wishlist product_id="24746"]
So the “24746” part is pulled directly from the id parameter in the URL. Essentially, I need the product ID to be inserted automatically into the YITH Wishlist shortcode based on the query parameter.
Has anyone done something similar or have any ideas on how best to achieve this? Any guidance on how to detect the id in the URL and then pass it into [yith_wcwl_add_to_wishlist product_id=""] would be greatly appreciated.
Thank you in advance for your help! I ping bellow my current snippet. If you need any more details, let me know.
-----------
My current snippet :
<div id="wishlist-button-container" class="wishlist-button-container">
[yith_wcwl_add_to_wishlist product_id="34603"]
</div>
<script>
// Permit to run the snippet when the elementor popup get open otherwise it result in error, because this snippet reside in a elementor popup that get triggered if a product is click on my shop page, the popup get populated with dynamic data from the url
jQuery(document).on('elementor/popup/show', function(event, id, instance) {
if (id === 39134) {
// Check if the YITH Wishlist global object is available
if (typeof YITH_WCWL !== 'undefined') {
// 1. Call YITH_WCWL.init() if it exists
if (typeof YITH_WCWL.init === 'function') {
YITH_WCWL.init();
}
// 2. Call get_ajax_fragments() if it exists (refresh fragments)
if (typeof YITH_WCWL.get_ajax_fragments === 'function') {
YITH_WCWL.get_ajax_fragments();
}
}
// 3. Manually trigger the YITH reload event in case the plugin is listening for it
jQuery(document).trigger('yith_wcwl_reload_fragments');
}
});
</script>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.