Hey I developed a form in which user will add detial and it will send email to the team using wp mail. Now i have two issue one is i am having redirect issue i want to redirect user to back page after email sent but it is not working. Other Issue is that I am trying to send product detail with email but i see only email full name and some msg not product detail in the message please check if u can help
<?php
/*
Template Name: Product Request Form
*/
get_header();
?>
<div class="product-request-form">
<br>
<br>
<h3>Request Price</h3>
<br>
<hr>
<form id="request-form" method="post">
<div class="form-row">
<input type="text" name="full-name" id="full-name" placeholder="Full Name" required>
</div>
<div class="form-row">
<input type="email" name="email" id="email" placeholder="Email" required>
</div>
<div class="form-row">
<textarea name="message" id="message" placeholder="Message" rows="10" required></textarea>
</div>
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="submit" value="Request Price" name="submit-request">
</form>
<?php
// Check if a product ID is provided in the URL
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : 0;
if ($product_id > 0) {
// Retrieve product information and display it
$product = wc_get_product($product_id);
if ($product) {
echo '<div class="product-details-row">';
echo '<div class="product-image">' . $product->get_image() . '</div>';
echo '<br>';
echo '<div class="product-info">';
echo '<h3>' . $product->get_name() . '</h3>';
echo '<p>' . $product->get_description() . '</p>';
// Add more product details as needed.
echo '</div>';
echo '</div>';
}
}
?>
</div>
<style>
.form-row input[type="text"],.form-row input[type="email"],.form-row textarea {
width: 100%;
}
input[type="submit"]{
margin-left:3px;
margin-bottom:30px;
margin-top:20px;
}
.request
</style>
<?php
if (isset($_POST['submit-request'])) {
$full_name = sanitize_text_field($_POST['full-name']);
$email = sanitize_email($_POST['email']);
$message = sanitize_text_field($_POST['message']);
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
// Send the email to the predefined email address
$to = '[email protected]'; // Change to your email address
$subject = 'Product Request';
$headers = array('Content-Type: text/html; charset=UTF-8');
// Construct the email message
$email_message = "Full Name: $full_name<br>";
$email_message .= "Email: $email<br>";
$email_message .= "Message: $message<br>";
if ($product_id > 0) {
$product = wc_get_product($product_id);
if ($product) {
$email_message .= "<br>Product Details:<br>";
$email_message .= "Name: " . $product->get_name() . "<br>";
$email_message .= "Price: " . wc_price($product->get_price()) . "<br>";
$email_message .= "Description: " . $product->get_description() . "<br>";
// Add more product details as needed.
}
}
sleep(2);
$sent = wp_mail($to, $subject, $email_message, $headers);
if ($sent) {
// Redirect to the custom URL after sending the email
wp_redirect('https://thebrief-sa.com/product/' . sanitize_title($product->get_name()) . '/');
exit();
} else {
echo '<p class="error-message">There was an error sending your request. Please try again later.</p>';
}
}
get_footer();
?>
u can add Your Email to test here is the function i am using on onTap of a button
function custom_request_price_button() {
global $product;
if ($product->get_price() == 0) {
$product_id = $product->get_id();
$request_form_page_url = 'https://thebrief-sa.com/product-request-form/?product_id=' . $product_id;
return '<a class="request-price-button" style="background-color: black; color: white; padding:10px" href="' . esc_url($request_form_page_url) . '">Request Price</a>';
}
return '';
}
add_shortcode('request_price_button', 'custom_request_price_button');