اضافه به لیست علاقمندیها

با این کد می‌توانید یک سیستم سبک و اختصاصی علاقه‌مندی برای ووکامرس ایجاد کنید که شامل دکمه قلب، ذخیره محصولات، نمایش لیست علاقه‌مندی و حذف سریع محصولات است.

اضافه کردن محصولات به لیست علاقه‌مندی‌های ووکامرس

این کد یک سیستم لیست علاقه‌مندی اختصاصی برای ووکامرس ایجاد می‌کند و بدون نیاز به افزونه جانبی، امکان ذخیره محصولات موردعلاقه کاربران را فراهم می‌سازد.

با شورت‌کد [wishlist_heart] می‌توان دکمه قلب را در صفحه محصول قرار داد. با کلیک روی آن، محصول به لیست علاقه‌مندی اضافه یا از آن حذف می‌شود.

همچنین شورت‌کد [my_wishlist] برای نمایش لیست محصولات ذخیره‌شده استفاده می‌شود و اطلاعاتی مانند تصویر، نام، قیمت و امکان حذف محصول را نمایش می‌دهد.

اطلاعات علاقه‌مندی هر کاربر در متای کاربری _user_wishlist ذخیره می‌شود و عملیات افزودن و حذف محصولات نیز با AJAX انجام می‌گیرد؛ بنابراین صفحه نیازی به بارگذاری مجدد ندارد.


<?php
function wishlist_get_svgs(){
    return [
        'white' => '<svg width="23" height="23" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 21.35L10.55 20.03C5.4 15.36 2 12.28 2 8.5C2 5.42 4.42 3 7.5 3C9.24 3 10.91 3.81 12 5.09C13.09 3.81 14.76 3 16.5 3C19.58 3 22 5.42 22 8.5C22 12.28 18.6 15.36 13.45 20.04L12 21.35Z" fill="#9CA3AF"/></svg>',
        'red'   => '<svg width="23" height="23" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 21.35L10.55 20.03C5.4 15.36 2 12.28 2 8.5C2 5.42 4.42 3 7.5 3C9.24 3 10.91 3.81 12 5.09C13.09 3.81 14.76 3 16.5 3C19.58 3 22 5.42 22 8.5C22 12.28 18.6 15.36 13.45 20.04L12 21.35Z" fill="#FF0000"/></svg>',
        'remove'=> '<svg width="23" height="23" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M18 6L6 18M6 6L18 18" stroke="#FF0000" stroke-width="3" stroke-linecap="round"/></svg>'
    ];
}
add_action('wp_ajax_toggle_wishlist','wishlist_toggle');
add_action('wp_ajax_nopriv_toggle_wishlist','wishlist_toggle');
function wishlist_toggle(){
    check_ajax_referer('wishlist_nonce','nonce');
    if(!is_user_logged_in()){
        wp_send_json_error(['message'=>'not_logged_in']);
    }
    $user_id = get_current_user_id();
    $product_id = intval($_POST['product_id']);
    $wishlist = (array) get_user_meta($user_id,'_user_wishlist',true);
    if(in_array($product_id,$wishlist)){
        $wishlist = array_diff($wishlist,[$product_id]);
        $added = false;
    }else{
        $wishlist[] = $product_id;
        $added = true;
    }
    update_user_meta($user_id,'_user_wishlist',$wishlist);
    wp_send_json_success(['added'=>$added]);
}
add_shortcode('wishlist_heart', function(){
    global $product;
    if(!$product && is_product()){
        $product = wc_get_product(get_the_ID());
    }
    if(!$product) return '';
    $product_id = $product->get_id();
    $svgs = wishlist_get_svgs();
    $in_list = false;
    if(is_user_logged_in()){
        $wishlist = (array) get_user_meta(get_current_user_id(),'_user_wishlist',true);
        $in_list = in_array($product_id,$wishlist);
    }
    $icon = $in_list ? $svgs['red'] : $svgs['white'];
    $class = is_user_logged_in() ? 'wishlist-btn logged-in' : 'wishlist-btn guest';
    return '<span class="'.$class.'" data-id="'.$product_id.'">'.$icon.'</span>';
});
add_shortcode('my_wishlist', function(){
    if(!is_user_logged_in())
        return '<p>لطفاً وارد سایت شوید.</p>';
    $wishlist = (array) get_user_meta(get_current_user_id(),'_user_wishlist',true);
    if(empty($wishlist))
        return '<p>لیست علاقمندی شما خالی است.</p>';
    $svgs = wishlist_get_svgs();
    $html = '<div class="wishlist-table-wrap"><table class="wishlist-table"><thead>
    <tr><th>تصویر</th><th>نام محصول</th><th>قیمت</th><th>حذف</th></tr>
    </thead><tbody>';
    foreach(array_reverse($wishlist) as $pid){
        $product = wc_get_product($pid);
        if(!$product) continue;
        $html .= '<tr>
            <td><a href="'.get_permalink($pid).'">'.$product->get_image().'</a></td>
            <td><a href="'.get_permalink($pid).'">'.$product->get_name().'</a></td>
            <td>'.$product->get_price_html().'</td>
            <td><span class="wishlist-btn logged-in" data-id="'.$pid.'">'.$svgs['remove'].'</span></td>
        </tr>';
    }
    $html .= '</tbody></table></div>';
    return $html;
});
add_action('wp_footer', function(){
    if(!is_product() && !is_shop() && !is_product_category() && !is_account_page())
        return;
    $svgs = wishlist_get_svgs();
    ?>
<style>
.wishlist-btn{
    cursor:pointer;
    display:inline-flex;
    align-items:center;
    justify-content:center;
    margin:0!important;
    line-height:0 !important;
    font-family:iranyekan, sans-serif !important;
}
.wishlist-btn svg{
    display:block !important;
}
.wishlist-table-wrap{
    overflow-x:auto;
    -webkit-overflow-scrolling:touch;
    width:100%;
}
.wishlist-table{
    width:100%;
    border-collapse:collapse;
    font-family:iranyekan, sans-serif !important;
    min-width:600px;
    margin:0 !important;
}
.wishlist-table thead th{
    background-color: #f5f5f5 !important;
}
.wishlist-table th,
.wishlist-table td{
    border:1px solid #eee;
    padding:10px;
    text-align:center;
    background-color: #ffffff !important;
}
.wishlist-table img{
    max-width:70px;
    border-radius:5px;    
    display: block;     
    margin: 0 auto !important;
}
.wl-toast{
    position:fixed;
    bottom:25px;
    left:50%;
    transform:translateX(-50%);
    background:#DDA63A;
    color:#fff;
    padding:10px 20px;
    border-radius:5px;
    font-size:14px;
    opacity:0;
    pointer-events:none;
    transition:all .4s ease;
    z-index:99999;
    font-family:iranyekan, sans-serif !important;
    white-space: nowrap;
    width: max-content;
    max-width: 90%;
}
.wl-toast.show{
    opacity:1;
    bottom:45px;
}
@media (max-width: 768px){
    .wishlist-table-wrap{
        overflow-x:auto;
        -webkit-overflow-scrolling:touch;
    }
    .wishlist-table{
        min-width:350px;
    }
}
</style>
<div id="wl-toast" class="wl-toast"></div>
<script>
jQuery(function($){
    function wlToast(msg){
        let box = $("#wl-toast");
        box.text(msg).addClass("show");
        setTimeout(()=> box.removeClass("show"), 1800);
    }
    $(document).on('click','.wishlist-btn.logged-in',function(e){
        e.preventDefault();
        var el = $(this);
        var product_id = el.data('id');
        $.post('<?php echo admin_url('admin-ajax.php'); ?>',{
            action:'toggle_wishlist',
            product_id:product_id,
            nonce:'<?php echo wp_create_nonce("wishlist_nonce"); ?>'
        },function(res){
            if(!res.success) return;
            if(res.data.added){
                el.html(`<?php echo $svgs['red']; ?>`);
                wlToast("به علاقه مندی اضافه شد");
            }else{
                el.html(`<?php echo $svgs['white']; ?>`);
                wlToast("از علاقه مندی حذف شد");

                if(el.closest('tr').length){
                    el.closest('tr').fadeOut(300,function(){
                        $(this).remove();
                    });
                }
            }

        },'json');
    });
    $(document).on('click','.wishlist-btn.guest',function(e){
        e.preventDefault();
        wlToast("برای استفاده از لیست علاقه مندی ها وارد شوید");
    });
});
</script>
<?php
});

روش اول: فایل functions.php

اگر از قالب فرزند (Child Theme) استفاده می‌کنید، می‌توانید کد را در فایل: functions.php قالب فرزند قرار دهید.

توصیه می‌شود کد را مستقیماً در فایل functions.php قالب اصلی قرار ندهید؛ زیرا با به‌روزرسانی قالب ممکن است تغییرات شما از بین برود.

روش دوم: استفاده از افزونه Code Snippets

روش ساده‌تر این است که از افزونه‌های مدیریت Snippet استفاده کنید.

در این روش، کد را به‌عنوان یک Snippet جدید اضافه کرده و آن را فعال می‌کنید.

مزیت این روش این است که برای اضافه کردن کدهای PHP نیازی به ویرایش مستقیم فایل‌های قالب ندارید.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *