'Add new value at the end of URL Woo Elementor

I want to change following code from Elementor:

    $this->add_render_attribute( 'button',
        [
            'rel' => 'nofollow',
            'href' => $product->add_to_cart_url(),
            'data-quantity' => ( isset( $settings['quantity'] ) ? $settings['quantity'] : 1 ),
            'data-product_id' => $product->get_id(),
            'class' => $class,
        ]
    );

to add new value like this:

foreach (get_the_terms(get_the_ID(), 'producer') as $cat) {
   echo $cat->name;
}

for the 'href' part in first code:

'href' => $product->add_to_cart_url(),

above 'href' code adds this value in path ?add-to-cart=2809 and I want to make it look like this ?add-to-cart=2809&wdm_name=MY-CODE-FOR-EACH-ABOVE-CREATE-VALUE-HERE

How do I implement second code inside HREF part so I dont get the error.

Thanks



Solution 1:[1]

Actually, just a few days ago I had the need of changing existing URLs inside href for each category on a widget my shop page.

So for my case I ended up creating this code:

var $ = jQuery;
$(document).ready(function(){
    var search = window.location.search; // Get all existing search params in URL
    var urlOrigin = document.location.origin; // Get current URL (only domain)
    //console.log(urlOrigin); // For testing in browser console
    $('.widget-area .product-categories li a').each(function(){ // You can mobify this to get your elements where you have them listed
        var catSlug = $(this).attr('href').replace(urlOrigin,''); // I am doing this because I need to get the slug for each category to then append to the final URL
        url = urlOrigin+catSlug+search; // Final URL mounted, here is where you can put your variable to set the code you want for each URL
        $(this).attr('href',url); // Associate this new generated URL to the element
    });
});

Since it is not clear where/how you want to use it, I leave my code here in case you can use it to adapt to your case, which I belive you can.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pedro