Skip to content

Instantly share code, notes, and snippets.

@TanvirHasan19
Created December 13, 2025 09:31
Show Gist options
  • Select an option

  • Save TanvirHasan19/15abbf5e720f706323ea811bc043fb94 to your computer and use it in GitHub Desktop.

Select an option

Save TanvirHasan19/15abbf5e720f706323ea811bc043fb94 to your computer and use it in GitHub Desktop.
Fix for WWOF product sorting duplication issue
/**
* Fix for WWOF product sorting duplication issue
* This ensures the product list container is cleared before new products are added
*/
add_action( 'wp_footer', function() {
if ( is_page() || is_singular() ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Monitor for product list updates
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
// Check if we're in the WWOF form
var $wwofForm = $('.wwof-order-form, [data-wwof-form]');
if ($wwofForm.length) {
// Find product table containers
var $productContainers = $wwofForm.find('tbody, .product-list, [data-products]');
$productContainers.each(function() {
var $container = $(this);
var productRows = $container.find('tr, .product-row, [data-product-id]');
// If we detect duplicate product IDs, remove duplicates
var seenIds = {};
productRows.each(function() {
var $row = $(this);
var productId = $row.data('product-id') ||
$row.find('[data-product-id]').first().data('product-id') ||
$row.attr('id');
if (productId) {
if (seenIds[productId]) {
// This is a duplicate, remove it
$row.remove();
} else {
seenIds[productId] = true;
}
}
});
});
}
}
});
});
// Start observing
if (document.body) {
observer.observe(document.body, {
childList: true,
subtree: true
});
}
});
</script>
<?php
}
}, 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment