افزودن پاپآپ بعد از اضافه شدن محصول به سبد خرید ووکامرس
این کد برای ووکامرس یک پاپآپ سفارشی ایجاد میکند که پس از اضافه شدن محصول به سبد خرید نمایش داده میشود.
در صورت اضافه شدن موفق محصول، پیام «محصول به سبد خرید اضافه شد!» همراه با دو گزینه «تسویه حساب» و «ادامه خرید» نمایش داده میشود.
همچنین اگر کاربر محصولی را که قبلاً در سبد خرید دارد دوباره اضافه کند، عملیات متوقف شده و پاپآپی با پیام «این محصول در سبد خرید شما موجود است!» نمایش داده میشود که گزینه ورود به سبد خرید را نیز دارد.
نمایش پاپآپ با استفاده از Session ووکامرس مدیریت شده و برای ظاهر آن نیز CSS و برای بستن پاپآپ JavaScript اختصاصی در نظر گرفته شده است.
<?php
add_filter( 'wc_add_to_cart_message_html', '__return_null' );
add_filter('woocommerce_add_to_cart_validation', 'check_product_in_cart_before_add', 10, 3);
function check_product_in_cart_before_add($passed, $product_id, $quantity) {
if (!empty(WC()->cart)) {
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $product_id) {
if (!empty(WC()->session)) {
WC()->session->set('cart_error_popup', true);
}
return false;
}
}
}
return $passed;
}
function cart_popup_after_page_reload() {
if (!empty(WC()->session)) {
if (WC()->session->get('cart_error_popup')) {
?>
<div id="custom-cart-popup" class="custom-popup-overlay">
<div class="custom-popup-content">
<div class="popup-icon error-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="#c62828" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</div>
<p>این محصول در سبد خرید شما موجود است!</p>
<button type="button" onclick="window.location.href='<?php echo esc_url(wc_get_cart_url()); ?>';" > سبد خرید </button>
<button class="custom-btn btn-secondary custom-close-popup">ادامه خرید</button>
</div>
</div>
<?php
WC()->session->set('cart_error_popup', false);
}
elseif (WC()->session->get('added_to_cart_popup')) {
?>
<div id="custom-cart-popup" class="custom-popup-overlay">
<div class="custom-popup-content">
<div class="popup-icon success-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="#2e7d32" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M20 6L9 17l-5-5"/>
</svg>
</div>
<p>محصول به سبد خرید اضافه شد!</p>
<button type="button" onclick="window.location.href='<?php echo esc_url(wc_get_checkout_url()); ?>';" > تسویه حساب </button>
<button class="custom-btn btn-secondary custom-close-popup">ادامه خرید</button>
</div>
</div>
<?php
WC()->session->set('added_to_cart_popup', false);
}
}
}
add_action('wp_footer', 'cart_popup_after_page_reload');
function set_cart_popup_session($cart_item_key, $product_id) {
if (!empty(WC()->session)) {
WC()->session->set('added_to_cart_popup', true);
}
}
add_action('woocommerce_add_to_cart', 'set_cart_popup_session', 10, 2);
add_action('wp_head', function() {
?>
<style>
.custom-popup-overlay {
position: fixed;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(0,0,0,0.4);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
backdrop-filter: blur(2px);
}
.custom-popup-content {
background: #fff;
padding: 10px;
border-radius: 10px;
text-align: center;
box-shadow: 0 20px 60px rgba(0,0,0,0.15);
max-width: 300px;
width: 90%;
animation: popupFadeIn 0.3s ease-out;
}
@keyframes popupFadeIn {
from { transform: translateY(-20px) scale(0.95); opacity: 0; }
to { transform: translateY(0) scale(1); opacity: 1; }
}
.popup-icon {
width: 50px; height: 50px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 10px;
}
.success-icon { background: #e8f5e9; }
.error-icon { background: #fbe9e7; }
</style>
<?php
});
add_action('wp_footer', function() {
?>
<script>
jQuery(function($){
$(document).on('click', '.custom-close-popup', function() {
$('#custom-cart-popup').fadeOut(300);
});
$(document).on('click', '#custom-cart-popup', function(e) {
if ($(e.target).is('#custom-cart-popup')) {
$(this).fadeOut(300);
}
});
});
</script>
<?php
});