Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnanthFlycart/9a24db66319f4ef8ef7b7c3df7d8b94d to your computer and use it in GitHub Desktop.
Save AnanthFlycart/9a24db66319f4ef8ef7b7c3df7d8b94d to your computer and use it in GitHub Desktop.
UpsellWP - Redirect to product page when click add to cart button in upsell popup
add_action('wp_footer', function () { ?>
<script>
jQuery(document.body).on('click', ".cuw-modal .cuw-popup-products .cuw-product .cuw-add-product-to-cart", function () {
let product_id = jQuery(this).closest('.cuw-product').data('id');
jQuery(this).closest('.cuw-product').data('id', 0);
jQuery.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'cuw_get_permalink',
product_id: product_id
},
success: function (response) {
if (response.data) {
window.location.href = response.data;
}
},
});
});
</script>
<?php
}, 1);
add_action('wp_ajax_cuw_get_permalink', 'cuw_get_permalink');
add_action('wp_ajax_nopriv_cuw_get_permalink', 'cuw_get_permalink');
if (!function_exists('cuw_get_permalink')) {
function cuw_get_permalink()
{
if (!empty($_POST['product_id'])) {
$product_url = get_permalink((int)$_POST['product_id']);
if (!empty($product_url)) {
wp_send_json_success($product_url);
}
}
wp_send_json_error();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment