Skip to content
Advertisement

How to add id to button add to woocomerce cart

someone knows how I can add id to the woocommerce buttons, woocommerce adds this structure

<button type="submit" data-quantity="1" data-product_id="id_del_producto" 
    class="button alt ajax_add_to_cart add_to_cart_button product_type_simple" >
       Agregar al Carrito
</button>

But do not add an ID to the buttons, I would like to know how you could add the product id to the id of the button, that is, if the product id is 604 to add an id = "id604" or similar to the button so that the structure that generates

<button type="submit" data-quantity="1" data-product_id="id_del_producto" 
    class="button alt ajax_add_to_cart add_to_cart_button product_type_simple" id="id604">
     Agregar al Carrito
</button>

this has to be on all the buttons, I think I have to modify the add-to-cart.php file but I don’t know how to do it, I’m not very good at php, I appreciate your answers, thanks!

Advertisement

Answer

If its not the add_to_cart.php try simple.php you will find all buttons variations in wp-content/plugins/woocommerce/templates/single-product/add-to-cart or if you are using a theme supporting woocommerce try to find it in your theme’s woocommerce folder.

Then you simply add this line of code id="id<?php echo esc_attr( $product->get_id() ); ?>"

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement