Reputation: 101
In Woocommerce, i need to open a modal popup when the user clicks the Add to cart button. I tried this, but it doesn't work:
In functions.php:
if (is_woocommerce() && is_archive()) {
wp_enqueue_script( 'frontend-custom', get_template_directory_uri() . '/js/frontend-custom.js', array("jquery"));
add_thickbox();
}
In themedirectory/js/frontend-custom.js:
jQuery(document).ready(function($) {
$('body').on('added_to_cart',function(e,data) {
//alert('Added ' + data['div.widget_shopping_cart_content']);
if ($('#hidden_cart').length == 0) { //add cart contents only once
//$('.added_to_cart').after('<a href="#TB_inline?width=600&height=550&inlineId=hidden_cart" class="thickbox">View my inline content!</a>');
$(this).append('<a href="#TB_inline?width=300&height=550&inlineId=hidden_cart" id="show_hidden_cart" title="<h2>Cart</h2>" class="thickbox" style="display:none"></a>');
$(this).append('<div id="hidden_cart" style="display:none">'+data['div.widget_shopping_cart_content']+'</div>');
}
$('#show_hidden_cart').click();
});
});
Any help appreciated.
Upvotes: 4
Views: 8703
Reputation: 11
The popup modal only works with Ajax, so you hnave to use Ajax for the add to cart function first.
This post is perfectly explaining how to do that: https://quadmenu.com/add-to-cart-with-woocommerce-and-ajax-step-by-step/
Works for me!
Upvotes: 1