fbpx
Want To Add Extra Images Per Product Variation? Download This Free Plugin

Variation image on custom archive page

John Byrne

I have a custom archive page and the swatches are3 working great but the image swap isn’t working ?

I am using the shortcode ?

<?php echo do_shortcode(‘[wvs_show_archive_variation]’); ?>

John Byrne

Hi I have managed to get the swatches working now on my custom archive page. I have managed to coder it so tat regardless of availability etc as you cant see prices till you are logged in nit i have overridden the unavailable etc so not blurred but ideally i need them to always display and allow the switching of the image as if you were logged in. ?

I know taht when i am not logged in as i am using CSP pricing in theory it looks like here is no price etc in what would be the usual price field. Is there a hook or can you guide me in the right direction so i can stop it saying unavailable when i click the swatch as it is nearly doing what i want below is my code that i have used to get the swatched to appear when logged out

// Force dummy prices and add a debug flag for variations for logged-out users
add_filter( ‘woocommerce_available_variation’, ‘force_variation_data_for_swatches’, 99, 3 );
function force_variation_data_for_swatches( $variation_data, $product, $variation ) {
if ( ! is_user_logged_in() ) {
// Add a custom debug flag to the variation data (checkable via JS console or inspecting window.cspVariations)
$variation_data[‘dummy_debug’] = ‘Variation filter applied for variation ID: ‘.$variation->get_id();
// If the price is empty, assign dummy values so that the variation data is complete
if ( empty( $variation_data[‘price’] ) ) {
$variation_data[‘price’] = 0;
$variation_data[‘display_price’] = 0;
$variation_data[‘display_regular_price’] = 0;
// We hide the dummy price with inline CSS since you’ll be displaying your custom “Login to see pricing” message elsewhere.
$variation_data[‘price_html’] = ‘<span class=”dummy-price” style=”display:none;”>Login to see pricing</span>’;
}
// Force the variation to be considered purchasable (so swatches are not disabled)
$variation_data[‘is_purchasable’] = true;
}
return$variation_data;
}
// Also force variations to be marked as purchasable for logged-out users
add_filter(‘woocommerce_variation_is_purchasable’, ‘custom_variation_is_purchasable_for_guests’, 99, 2);
function custom_variation_is_purchasable_for_guests($purchasable, $variation) {
if ( ! is_user_logged_in() ) {
returntrue;
}
return$purchasable;
}
add_action(‘wp_footer’, ‘my_debug_echo’, 100);
function my_debug_echo(){
if ( ! is_user_logged_in() && ( is_product() || is_shop() || is_product_category() || is_product_tag() ) ) {
echo'<!– Debug: Variation filter is active and dummy prices are being applied for logged-out users –>’;
}
}
// Ensure variations appear as in-stock for logged-out users so swatches are enabled
add_filter(‘woocommerce_available_variation’, ‘force_variation_stock_for_swatches’, 99, 3);
function force_variation_stock_for_swatches( $variation_data, $product, $variation ) {
if ( ! is_user_logged_in() ) {
// Force variations to be in stock and assign a high stock quantity.
$variation_data[‘is_in_stock’] = true;
$variation_data[‘stock_quantity’] = 100; // Arbitrary high number
// Remove any “out of stock” messages from availability_html.
$variation_data[‘availability_html’] = ”;
}
return$variation_data;
}
function my_custom_wvs_archive_variation_shortcode($atts) {
// Get the original output from the plugin’s shortcode function.
$output = woo_variation_swatches_pro()->show_archive_variation_shortcode($atts);

// Only modify output for logged-out users.
if ( ! is_user_logged_in() ) {
// Remove ‘disabled’ class from swatch items.
$output = str_replace(‘disabled’, ”, $output);
// Remove text like “(Unavailable)” if present.
$output = preg_replace(‘/\(\s*Unavailable\s*\)/i’, ”, $output);
}

return$output;
}

// Unregister the original shortcode and re-register ours.
remove_shortcode(‘wvs_show_archive_variation’);
add_shortcode(‘wvs_show_archive_variation’, ‘my_custom_wvs_archive_variation_shortcode’);
function custom_js_enable_swatches_for_guests_observer() {
if ( ! is_user_logged_in() ) {
?>
<scripttype=”text/javascript”>
jQuery(document).ready(function($){
functionremoveDisabledSwatches() {
$(‘.archive-variable-items li.disabled’).each(function(){
$(this).removeClass(‘disabled’);
$(this).removeAttr(‘data-wvstooltip-out-of-stock’);
$(this).css(‘pointer-events’, ‘auto’);
});
}

functionbindSwatchClick() {
// Remove any previously bound events in our custom namespace
$(‘.archive-variable-items li’).off(‘click.mySwatch’).on(‘click.mySwatch’, function(e){
e.preventDefault();
varswatch = $(this);
varvalue = swatch.data(‘value’);
// Find the associated select element in the same wrapper
varwrapper = swatch.closest(‘.woo-variation-items-wrapper’);
varselect = wrapper.find(‘select.woo-variation-raw-select’);
if(select.length && value) {
// Set the value and trigger change so that the image swapping fires
select.val(value).trigger(‘change’);
}
});
}

functionreinitSwatchEvents() {
// If the plugin exposes a JS re-initialization method, call it.
if ( typeofWVS !== ‘undefined’ && WVS.archive && typeofWVS.archive.bindEvents === ‘function’ ) {
WVS.archive.bindEvents();
} else {
// Otherwise, trigger a custom event in case the plugin listens for it.
$(document.body).trigger(‘wvs_swatches_refresh’);
}
}

functioncleanupAndReinit() {
removeDisabledSwatches();
bindSwatchClick();
reinitSwatchEvents();
}

// Initial run
cleanupAndReinit();

// Monitor for dynamic changes with MutationObserver
varswatchContainers = document.querySelectorAll(‘.archive-variable-items’);
swatchContainers.forEach(function(container) {
varobserver = newMutationObserver(function(mutations, obs){
cleanupAndReinit();
});
observer.observe(container, { childList:true, subtree:true });
});

// Fallback check every second
setInterval(cleanupAndReinit, 1000);
});
</script>
<?php
}
}
add_action(‘wp_footer’, ‘custom_js_enable_swatches_for_guests_observer’, 100);

Hakik Zaman

Hi John,

I am sorry it is beyond our support scope.

Thank You