In WooCommerce, I have added a "subscribe" button in single product pages. It opens a popup window from an URL like:
https://elfromulario.com?sku=
where I add the product SKU as an URL variable.
But when I use it, the browsers block the pop-up window.
Is there a more functional way or a more efficient way to do it?
My code:
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_custom_content', 25 );
function woocommerce_template_custom_content(){
global $product;
$sku = $product->get_sku();
if (isset($_POST['subscriberc'])) {
// Redirect to with the SKU appended
$googleUrl = "https:https://googleurl.com/Formulario.php?SKU=" . $sku;
echo '<script>window.open("' . $googleUrl . '");</script>';
}
?><form method="post">
<input type="hidden" name="subscriberc" value="1">
<input style="
background: #034392 !important;
font-family: 'Open Sans', sans-serif;
font-size: 15px;
font-weight: bold;
width: 200px;
margin-bottom: 18px;
" type="submit" value="¡SUSCRIBITE AHORA!">
</form><?
}
2
Answers
I’m not surprised that any browser prevents redirecting from javascript.
You might prefer to redirect with PHP with something like :
Also : you have
https:
prepended to the URL string, maybe that won’t help 🙂Why just not using a simple linked button with a ‘_bank’ target to open the link in a new browser tab, instead of a complicated form, that reloads the page opening a popup that will be blocked anyway by the browser.
Try the following simple solution instead:
Code goes in functions.php file of the child theme (or in a plugin). Tested and works.
You will get something like (without a form, just a link):