Created
November 1, 2022 17:02
-
-
Save jmarreros/1df2904bac3de57943e93393be314919 to your computer and use it in GitHub Desktop.
Request a quote functionality in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // No copiar este línea | |
add_action('woocommerce_share', 'dcms_show_request_quote_form'); | |
function dcms_show_request_quote_form(){ | |
global $product; | |
if ( isset($_GET['sent']) ){ | |
$message = $_GET['sent'] ? 'Solicitud enviada' : 'Hubo un error'; | |
echo "<p class='msg-request-quote'>$message</p>"; | |
return; | |
} | |
?> | |
<br> | |
<h3>Solicita una cotización</h3> | |
<form id="frm-quote" method="post" action="<?= admin_url( 'admin-post.php' ) ?>"> | |
<label for="name">Nombre:</label> | |
<input type="text" name="name" id="name" required> | |
<br> | |
<label for="email">Correo:</label> | |
<input type="email" name="email" id="email" required> | |
<br> | |
<label for="message">Mensaje:</label> | |
<textarea name="message" id="message" rows="6" required></textarea> | |
<br> | |
<input type="checkbox" id="terms" name="terms" required> | |
<label id="lbl-terms" for="terms">Acepto los <a href="#">Términos y Condiciones</a></label> | |
<br> | |
<input type="hidden" name="sku_product" value="<?= $product->get_sku() ?>"> | |
<input type="hidden" name="url_product" value="<?= get_permalink( $product->get_id() ) ?>" > | |
<input type="hidden" name="action" value="process_form_quote"> | |
<input id="submit" type="submit" name="submit" value="Enviar"> | |
</form> | |
<?php | |
} | |
// Hooks admin-post | |
add_action( 'admin_post_nopriv_process_form_quote', 'dcms_mail_request_quote' ); | |
add_action( 'admin_post_process_form_quote', 'dcms_mail_request_quote' ); | |
function dcms_mail_request_quote() { | |
$name = sanitize_text_field($_POST['name']); | |
$email = sanitize_email($_POST['email']); | |
$message = sanitize_textarea_field($_POST['message']); | |
$sku_product = sanitize_text_field($_POST['sku_product']); | |
$url_product = sanitize_url($_POST['url_product']); | |
$adminmail = "[email protected]"; | |
$subject = "Solicitud cotización - producto $sku_product"; | |
$headers = "Reply-to: $name <$email>"; | |
$msg = "Nombre: $name \n"; | |
$msg .= "E-mail: $email \n\n"; | |
$msg .= "Mensaje: \n\n $message \n\n"; | |
$msg .= "Producto: $url_product \nSKU : $sku_product"; | |
$sendmail = wp_mail( $adminmail, $subject, $msg, $headers); | |
wp_redirect( home_url($url_product)."?sent=".$sendmail ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment