Created
March 2, 2026 18:56
-
-
Save tdmrhn/cc7a13324bde2027d68fb4df97ee8db5 to your computer and use it in GitHub Desktop.
Blocksy 2: Add Auto Open Review Tab/Accordion Link to Star Rating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script> | |
| document.addEventListener('click', function (e) { | |
| const ratingTrigger = e.target.closest( | |
| '.ct-woo-card-rating, .woocommerce-product-rating .star-rating' | |
| ); | |
| if (!ratingTrigger) return; | |
| const isSingle = document.body.classList.contains('single-product'); | |
| // SINGLE PRODUCT | |
| if (isSingle) { | |
| e.preventDefault(); | |
| openReviewsTab(); | |
| return; | |
| } | |
| // SHOP - ARCHIVE | |
| const productCard = ratingTrigger.closest('.product'); | |
| if (!productCard) return; | |
| const productLink = productCard.querySelector('a.woocommerce-LoopProduct-link'); | |
| if (!productLink) return; | |
| window.location.href = productLink.href.replace(/#.*$/, '') + '#reviews'; | |
| }); | |
| function openReviewsTab() { | |
| const reviewsPanel = document.querySelector('#tab-reviews'); | |
| if (!reviewsPanel) return; | |
| // Default WooCommerce Tabs | |
| const tabsWrapper = document.querySelector('.woocommerce-tabs, .wc-tabs-wrapper'); | |
| const reviewsTab = tabsWrapper?.querySelector('.reviews_tab'); | |
| if (reviewsTab) { | |
| tabsWrapper.querySelectorAll('.wc-tabs li') | |
| .forEach(li => li.classList.remove('active')); | |
| tabsWrapper.querySelectorAll('.woocommerce-Tabs-panel') | |
| .forEach(panel => panel.style.display = 'none'); | |
| reviewsTab.classList.add('active'); | |
| reviewsPanel.style.display = 'block'; | |
| setTimeout(() => { | |
| reviewsPanel.scrollIntoView({ | |
| behavior: 'smooth', | |
| block: 'start' | |
| }); | |
| }, 100); | |
| return; | |
| } | |
| // Blocksy Accordion Mode | |
| const accordionBtn = document.querySelector( | |
| '.ct-accordion-heading[data-target="#tab-reviews"]' | |
| ); | |
| if (accordionBtn) { | |
| if (accordionBtn.getAttribute('aria-expanded') === 'false') { | |
| accordionBtn.click(); | |
| } | |
| setTimeout(() => { | |
| reviewsPanel.scrollIntoView({ | |
| behavior: 'smooth', | |
| block: 'start' | |
| }); | |
| }, 200); | |
| } | |
| } | |
| // Auto open if landing with #reviews | |
| if (window.location.hash === '#reviews') { | |
| window.addEventListener('load', function () { | |
| setTimeout(openReviewsTab, 400); | |
| }); | |
| } | |
| </script> | |
| <style> | |
| .ct-woo-card-rating, | |
| .woocommerce-product-rating .star-rating { | |
| cursor: pointer; | |
| } | |
| </style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment