Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Request a quote functionality in WooCommerce
<?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>";
<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>
<label for="email">Correo:</label>
<input type="email" name="email" id="email" required>
<label for="message">Mensaje:</label>
<textarea name="message" id="message" rows="6" required></textarea>
<input type="checkbox" id="terms" name="terms" required>
<label id="lbl-terms" for="terms">Acepto los <a href="#">Términos y Condiciones</a></label>
<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">
// 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 = "";
$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