جلوگیری از پرداخت محصولات چند فروشنده در یک فاکتور ووکامرس
این کد برای سایتهای چندفروشندگی ووکامرس طراحی شده و اجازه نمیدهد محصولات متعلق به چند فروشنده مختلف بهصورت همزمان در یک سبد خرید قرار گرفته و در یک فاکتور پرداخت شوند.
هنگام اضافه کردن محصول جدید، فروشنده محصول با فروشنده محصولات موجود در سبد خرید مقایسه میشود. اگر فروشنده متفاوت باشد، اضافه کردن محصول متوقف شده و یک پاپآپ هشدار به کاربر نمایش داده میشود.
در این پاپآپ به کاربر اعلام میشود که برای خرید از فروشنده دیگر باید سفارش جداگانهای ثبت کند و دو گزینه نیز در اختیار او قرار میگیرد:
متوجه شدم
مشاهده سبد خرید
این کد برای فروشگاههای چندفروشندگی ووکامرس بسیار کاربردی است و باعث میشود هر سفارش فقط شامل محصولات یک فروشنده باشد.
<?php
if (!defined('ABSPATH')) {
exit;
}
if (!defined('GPT_SINGLE_VENDOR_CART_POPUP_LOADED')) {
define('GPT_SINGLE_VENDOR_CART_POPUP_LOADED', true);
add_filter('woocommerce_add_to_cart_validation','gpt_block_multi_vendor_cart_popup',10,5);
add_action('wp_footer','gpt_single_vendor_cart_popup_html');
function gpt_block_multi_vendor_cart_popup($passed,$product_id,$quantity,$variation_id=0,$variations=array()){
if(!$passed){
return false;
}
if(!function_exists('WC') || !WC()->cart){
return $passed;
}
$new_product_id = $variation_id ? $variation_id : $product_id;
$new_vendor_id = gpt_get_product_vendor_for_cart_popup($new_product_id);
if(!$new_vendor_id){
return $passed;
}
foreach(WC()->cart->get_cart() as $cart_item){
if(empty($cart_item['product_id'])){
continue;
}
$cart_product_id = !empty($cart_item['variation_id'])
? $cart_item['variation_id']
: $cart_item['product_id'];
$cart_vendor_id = gpt_get_product_vendor_for_cart_popup($cart_product_id);
if($cart_vendor_id && $cart_vendor_id !== $new_vendor_id){
wc_add_notice(
'<span class="gpt-multi-vendor-cart-notice">multi_vendor</span>',
'error'
);
return false;
}
}
return $passed;
}
function gpt_get_product_vendor_for_cart_popup($product_id){
$product_id = absint($product_id);
if(!$product_id){
return 0;
}
if(function_exists('wc_get_product')){
$product = wc_get_product($product_id);
if($product && $product->is_type('variation')){
$product_id = $product->get_parent_id();
}
}
$post = get_post($product_id);
if($post && !empty($post->post_author)){
return (int)$post->post_author;
}
return 0;
}
function gpt_single_vendor_cart_popup_html(){
if(is_admin()){
return;
}
?>
<style>
.gpt-multi-vendor-cart-notice{ display:none; }
.woocommerce-error:has(.gpt-multi-vendor-cart-notice){ display:none !important; }
.gpt-vendor-popup-overlay{ position:fixed; inset:0; z-index:999999; background:rgba(0,0,0,.35); backdrop-filter:blur(4px); display:none; align-items:center; justify-content:center; padding:20px; }
.gpt-vendor-popup-overlay.active{ display:flex; }
.gpt-vendor-popup-box{ width:450px; max-width:100%; background:#fff; border-radius:10px; padding:10px; text-align:center; direction:rtl; box-shadow:0 25px 60px rgba(0,0,0,.2); animation:gptPopup .25s ease; }
@keyframes gptPopup{ from{transform:translateY(10px);opacity:0} to{transform:translateY(0);opacity:1} }
.gpt-vendor-popup-icon{ width:50px; height:50px; margin:auto; margin-bottom:10px; border-radius:50%; background:#fff3e0; display:flex; align-items:center; justify-content:center; font-size:30px; color:#ff9800; }
</style>
<div class="gpt-vendor-popup-overlay" id="gptVendorPopup">
<div class="gpt-vendor-popup-box">
<div class="gpt-vendor-popup-icon">!</div>
<strong>خرید محصولات چند فروشنده در یک فاکتور مجاز نمی باشد</strong>
<p> برای محصولات فروشنده دیگر لازم است سفارش جداگانه ثبت نمایید </p>
<button id="gptClosePopup"> متوجه شدم </button>
<button id="gptCartBtn"> مشاهده سبد خرید </button>
</div>
</div>
<script>
(function($){
function openPopup(){
$('#gptVendorPopup').addClass('active');
$('body').css('overflow','hidden');
}
function closePopup(){
$('#gptVendorPopup').removeClass('active');
$('body').css('overflow','');
}
function detectNotice(){
if($('.gpt-multi-vendor-cart-notice').length){
$('.gpt-multi-vendor-cart-notice').closest('.woocommerce-error').hide();
openPopup();
}
}
$(document).on('click','#gptClosePopup',function(){
closePopup();
});
$(document).on('click','#gptCartBtn',function(){
window.location.href="<?php echo esc_url(wc_get_cart_url()); ?>";
});
$(document).on('click','#gptVendorPopup',function(e){
if(e.target===this){
closePopup();
}
});
$(document.body).on('added_to_cart wc_fragments_refreshed updated_wc_div',function(){
detectNotice();
});
$(document).ready(function(){
detectNotice();
});
})(jQuery);
</script>
<?php
}
}