Skip to content

Instantly share code, notes, and snippets.

  • Save FrancoStino/fcf495d6e8b854213e1dbd29c92ea265 to your computer and use it in GitHub Desktop.
Save FrancoStino/fcf495d6e8b854213e1dbd29c92ea265 to your computer and use it in GitHub Desktop.
Add "Notify Me When Available/Waitlist" Into Single Product Page, Save In Ajax, Response Html Css Message And Send Email If Product Back In Stock Using Wc Email Template - Woocommerce
<?php
/*
* Add "Notify Me When Available" - Woocommerce
*/
add_action( 'woocommerce_single_product_summary', 'cwpai_notify_me_when_available', 30 );
function cwpai_notify_me_when_available() {
global $product;
if ( ! $product->is_in_stock() ) {
?>
<div class="notify-me-when-available" style="position: relative; z-index: 1; background-color: #fff; padding: 10px; border-radius: 10px;">
<label style="font-weight:600">Avvisami quando torna disponibile</label>
<?php if(!wp_is_mobile()): ?>
<form class="notify-me-when-available-form" style="display: flex;gap: 10px;flex-direction: row;" action="" method="post">
<input type="hidden" name="product_id" value="<?php echo $product->get_id(); ?>" />
<input style="width: 50%; background-color: #fff;" type="email" name="notify_email" placeholder="<?php esc_attr_e( 'Inserisci il tuo indirizzo email', 'cwpai' ); ?>" />
<input style="width: 50%;" class="btn btn-color-primary btn-style-default btn-style-semi-round btn-size-default" type="submit" name="notify_submit" value="<?php esc_attr_e( 'Invia', 'cwpai' ); ?>" />
<?php else: ?>
<form class="notify-me-when-available-form" style="display: flex;gap: 10px;flex-direction: column;" action="" method="post">
<input type="hidden" name="product_id" value="<?php echo $product->get_id(); ?>" />
<input style="width: 100%; background-color: #fff;" type="email" name="notify_email" placeholder="<?php esc_attr_e( 'Inserisci il tuo indirizzo email', 'cwpai' ); ?>" />
<input style="width: 100%;" class="btn btn-color-primary btn-style-default btn-style-semi-round btn-size-default" type="submit" name="notify_submit" value="<?php esc_attr_e( 'Invia', 'cwpai' ); ?>" />
<?php endif; ?>
</form>
<div class="notify-me-when-available-message"></div>
</div>
<script>
jQuery(function($) {
$('.notify-me-when-available-form').on('submit', function(e) {
e.preventDefault();
var $form = $(this);
var $message = $form.siblings('.notify-me-when-available-message');
$.ajax({
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
type: 'post',
dataType: 'json',
data: {
action: 'cwpai_notify_me_when_available',
product_id: $form.find('input[name="product_id"]').val(),
notify_email: $form.find('input[name="notify_email"]').val()
},
beforeSend: function() {
$message.removeClass().addClass('notify-me-when-available-message').html('<p style="padding: 15px 20px; margin: 20px 0;"><?php esc_html_e( 'Attendi...', 'cwpai' ); ?></p>');
},
success: function(response) {
if ( response.success ) {
$message.removeClass().addClass('notify-me-when-available-message success').html('<p style="padding: 15px 20px; margin: 20px 0; background-color: #caf3ca; border-radius: 10px; color: green;">' + response.data.message + '</p>');
} else {
$message.removeClass().addClass('notify-me-when-available-message error').html('<p style="padding: 15px 20px; margin: 20px 0; background-color: #f8e7e7; border-radius: 10px; color: #B50808;">' + response.data + '</p>');
}
}
});
});
});
</script>
<?php
}
}
add_action( 'wp_ajax_cwpai_notify_me_when_available', 'cwpai_notify_me_when_available_ajax' );
add_action( 'wp_ajax_nopriv_cwpai_notify_me_when_available', 'cwpai_notify_me_when_available_ajax' );
function cwpai_notify_me_when_available_ajax() {
$product_id = isset( $_POST['product_id'] ) ? intval( $_POST['product_id'] ) : 0;
$notify_email = isset( $_POST['notify_email'] ) ? sanitize_email( $_POST['notify_email'] ) : '';
if ( ! $product_id || ! $notify_email ) {
wp_send_json_error( __( 'Inserisci un indirizzo e-mail valido.', 'cwpai' ) );
}
$product = wc_get_product( $product_id );
if ( ! $product ) {
wp_send_json_error( __( 'Prodotto non trovato.', 'cwpai' ) );
}
$notify_emails = get_post_meta( $product_id, '_notify_emails', true );
if ( ! is_array( $notify_emails ) ) {
$notify_emails = array();
}
if ( in_array( $notify_email, $notify_emails ) ) {
wp_send_json_error( __( 'Ci dispiace, hai già inserito questa mail per essere avvisato quando il prodotto tornerà disponibile.', 'cwpai' ) );
}
$notify_emails[] = $notify_email;
update_post_meta( $product_id, '_notify_emails', $notify_emails );
wp_send_json_success( array( 'message' => __( 'Perfetto. Riceverai una mail quando il prodotto sarà di nuovo disponibile.', 'cwpai' ) ) );
}
add_action( 'woocommerce_product_set_stock_status', 'cwpai_notify_me_when_available_email', 10, 3 );
function cwpai_notify_me_when_available_email( $product_id, $status, $stock ) {
if ( 'instock' === $status ) {
$notify_emails = get_post_meta( $product_id, '_notify_emails', true );
if ( is_array( $notify_emails ) && ! empty( $notify_emails ) ) {
$mailer = WC()->mailer();
$product = wc_get_product( $product_id );
$subject = sprintf( __( 'È di nuovo disponibile il prodotto: %s', 'cwpai' ), $product->get_name() );
$message = sprintf( __( 'È di nuovo disponibile il prodotto: %s. Puoi acquistarlo direttamente cliccando su questo link: %s', 'cwpai' ), $product->get_name(), $product->get_permalink() );
$headers = "Content-Type: text/htmlrn";
foreach ( $notify_emails as $notify_email ) {
$mailer->send( $notify_email, $subject, $mailer->wrap_message( $subject, $message ), $headers );
}
delete_post_meta( $product_id, '_notify_emails' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment