WordPress

woocommerce offtopic – Przekierowanie Wp nie działa. Możesz pomóc

  • 6 listopada, 2023
  • 4 min read
woocommerce offtopic – Przekierowanie Wp nie działa. Możesz pomóc


Hej, opracowałem formularz, w którym użytkownik doda szczegóły, a on wyśle ​​wiadomość e-mail do zespołu za pomocą poczty wp. Teraz mam dwa problemy. Pierwszy to problem z przekierowaniem. Chcę przekierować użytkownika na stronę wstecz po wysłaniu wiadomości e-mail, ale to nie działa. Innym problemem jest to, że próbuję wysłać szczegóły produktu e-mailem, ale w wiadomości widzę tylko imię i nazwisko oraz wiadomość, która nie zawiera szczegółów produktu. Sprawdź, czy możesz pomóc

<?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(' . 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();
?>

możesz dodać swój adres e-mail do przetestowania. Oto funkcja, której używam po dotknięciu przycisku funkcja niestandardowe_request_price_button() { global $product;

if ($product->get_price() == 0) {
    $product_id = $product->get_id();
    $request_form_page_url=" . $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’);

Warto przeczytać!  załączniki — Jak załączyć obrazy tylko ze strony edycji postu


Źródło