Home Forums WoodMart support forum Custom JS & Product Variation Links

Custom JS & Product Variation Links

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #416994

    WASIPLtd_Website
    Participant

    Hello,

    I’m currently trying to resolve an issue for my customers where variation/attributed products aren’t generating unique URLs for the selected variations.

    For example, I offer a jacket in multiple sizes and colours (Small, Medium, Large | Orange, Yellow, Navy, Black), I want to send my customer a link to the Small, Yellow jacket that I have offered.

    Below are how I’d like the URLs to be generated within certain stages of the customer’s actions:

    URL when the customer clicks on the product image to be taken to the product page: https://google.com/product/winter-traffic-parka-4-refl-tape/

    URL when the customer clicks on the Small, Yellow variation of the jacket:
    https://google.com/product/winter-traffic-parka-4-refl-tape/?attribute_pa_jacket-colour=yellow&attribute_pa_size=small

    (I’ve changed the site URL to google for privacy reasons, but you should understand what I’m asking)

    I’ve found the following JS to assist with this issue, but it only seems to work when I’m logged in as an admin and is now showing an “add to cart button” even tho I have CSS and Catalogue Mode enabled.

    JS:

    /**
     * Little JS snippet to automatically update the WooCommerce single product page URL with the needed parameters for the active variations' selection.
     * 
     * Each time the user changes the variation selection, the URL is updated with the new parameters, so on hard refresh the selected variation is displayed.
     * 
     * Also, fast way to get the direct URL to the selected variation.
     * 
     * Paste it into your browser console and run it. Or use it in your theme/plugins.
     * 
     * @see https://stackoverflow.com/a/73138077/13765033
     */
    
    const els = document.querySelectorAll('.variations select')
    
    // add el.name and el.value as query parameters to the URL
    function updateURL(el) {
        const url = new URL(window.location.href)
        url.searchParams.set(el.name, el.value)
        window.history.pushState({}, '', url.href)
    }
    
    // update the URL when the user changes the select
    els.forEach(el => el.addEventListener('change', () => updateURL(el)))

    Please assist.

    #416995

    WASIPLtd_Website
    Participant

    I’ve got the JS to generate the unique URL now.

    However, the “Add to Cart” button has reappeared on all of my product pages.

    I need to have this hidden, I previously had CSS made to hide the button and it worked. But it’s now appearing again.

    I cannot enable “Catalogue Mode” because my site requires the option to showcase product variations.

    For more background information as to how my site is set up, please see here:

    https://xtemos.com/forums/topic/product-variations-attributes-not-showing-as-drop-down/

    #417012

    WASIPLtd_Website
    Participant

    It seems that only products with variations are now showing the button.

    Any product that doesn’t have a variation doesn’t show the “Add to Cart” button.

    Please assist.

    #417016

    WASIPLtd_Website
    Participant

    Issue resolved.

    Please ignore and close ticket.

    #417066

    Hello,

    You are Most Welcome.

    We are glad that you managed to solve the problem yourself. You are Great!!!

    Let us know if there’s anything else we can do for you! You can always reach us at any time. We are always here to help you.

    Have a wonderful day.

    Topic Closed.
    Best Regards.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Custom JS & Product Variation Links’ is closed to new replies.