G
G
gotohell2016-12-20 14:18:59
JavaScript
gotohell, 2016-12-20 14:18:59

How to store values ​​in div in cookies?

There is a website for food delivery, a basket is implemented there. I would like that when adding an item to the cart, it would be saved in cookies. That is, when you refresh the page or switch to another, the values ​​\u200b\u200bare saved! Found the jquery.cookie.js plugin but don't quite understand how to use it!

function cleanCart() {
  $.cookie('allCart', null);
  $('.mini_cart table.cartTable tbody').html("");
  $('.summ span').html('0');
  $('.numberGoods span').html('0');
  $('.checkoutBtn').hide();
  $('#but-basket').hide();
}
$( document ).ready(function() {
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$('.goods .cat').click(function() {
  var clicks = $(this).data('clicks');
  if (clicks) {
      $('.goods .cat .title').click(function() {
        $('.goods .cat .block').slideUp('200');
      });
  } else {
      $(this).find('.block').slideDown('200');
  }
  $(this).data("clicks", !clicks);
});
var cart = $('.numberGoods').clone();
var cartPage = $('.cartPage').clone();
$('.logoMob').after(cart);
cart.after(cartPage);
$('.top-panel .cartPage').empty();
$('.addToCartBtn').click(function () {
  swal('Товар успешно добавлен','','success');
});
}
  $('.permission .col-5').addClass("hidden").viewportChecker({
  classToAdd: 'visible animated bounceIn',
  offset: 100
  });
  $.cookie('allCart', null);
  $('.mini_cart table.cartTable tbody').html("");
  //$('.mini_cart table tbody').html($.cookie('allCart'));
  $('.addToCartBtn').on('click', function(){
      var totalCart = $(".mini_cart table.cartTable tbody > tr").length;
      $('.numberGoods span').html(totalCart+1);
      $('.numberGoods').addClass("animated bounceIn");
      var itemId = $(this).data('id');
      var itemPrice = $(this).data('price');
      var itemTitle = $(this).data('title');
      var itemQty = $(this).data('qty');
      $.cookie('cartId', itemId);
      $.cookie('cartPrice', itemPrice);
      $.cookie('cartTitle', itemTitle);
      $.cookie('cartQty', itemQty);
      var cartId = $.cookie('cartId');
      var cartPrice = $.cookie('cartPrice');
      var cartTitle = $.cookie('cartTitle');
      var cartQty = $.cookie('cartQty');
      var liHtml = '<tr><td>' + cartTitle + '</td><td>' + cartQty + '</td><td class="summEnd">' + cartPrice*cartQty + 'тг.</td></tr>';
      var allCart = $('.mini_cart table tbody').html();
      $('.mini_cart table.cartTable tbody').append(liHtml);
      if (totalCart >= 1) {
        $('.mini_cart .cleanCart').after('');;
      }
      else {
        $('.mini_cart .cleanCart').after('<div id="but-basket"><a href="javascript:void(0);" class="checkoutBtn" onclick="checkout()">Заказать</a></div>');
      }
      $.cookie('allCart', allCart);
      function second_passed() {
        $('.numberGoods').removeClass("animated bounceIn");
      }
      setTimeout(second_passed, 500);
      var sum = 0;
      $('table.cartTable tr').each(function(){
          $(this)
          .find('.summEnd')
          .each(function(){
              sum+=parseInt($(this).html());
          });
      });
      $('.summ span').html(sum);
      sum = 0;
  });
$( "input[name=quantity]" ).change(function() {
  var inpQty = $(this).val();
  $('.addToCartBtn').attr('data-qty',inpQty);
});
var maxHeight = 0;
$(".descGoods").each(function(){
  if ( $(this).height() > maxHeight )
  {
    maxHeight = $(this).height();
  }
});

$(".descGoods").height(maxHeight);
$(".single_add_to_cart_button").html('В корзину');
    function pagerFactory(idx, slide) {
        return '<li><a href="javascript:void(0);"></a></li>';
    };
});
$(function(){
    $(window).scroll(function() {
        var top = $(document).scrollTop();
        if (top > 100) {
          $(".fix").css({
            "position":"fixed",
            "padding-bottom":"0px",
            "width":"100%",
            "z-index":"999999999999999"
          });
          $('.top-panel').addClass('top-panelFix');
          $('.callbacks').addClass('callbacksFix');
          $('.top-panel .soc').addClass('socFix');
          $('.logo').addClass('logoFix');
          $('.phone').addClass('callbacksFix');
          $('.cart').addClass('cartFix');
        }
        else {
          $(".fix").css({
            "position":"relative",
          });
          $('.top-panel').removeClass('top-panelFix');
          $('.callbacks').removeClass('callbacksFix');
          $('.top-panel .soc').removeClass('socFix');
          $('.logo').removeClass('logoFix');
          $('.phone').removeClass('callbacksFix');
          $('.cart').removeClass('cartFix');
        }
    });
});
function openCart() {
  $('.cartPage').slideDown('200');
}
function closeCart() {
  $('.cartPage').slideUp('200');
}
function checkout() {
  var checkout = $('.cartPage .checkout').show();
  $('.mini_cart').hide();
  $('.closeCart').after(checkout);
}
function returnBack() {
  var checkout = $('.cartPage .mini_cart').show();
  $('.checkout').hide();
  $('.closeCart').after(checkout);
}
function send()
{
//Получаем параметры
var goods = $('.cartTable tbody').html();
//var goods = goods.replace(/\s+/g,' ');
var name = $('#name').val();
var phone = $('#phone').val();
var email = $('#email').val();
var summa = $('.summ span').html();
var delivery = $('#delivery').val();
if(name == "" || phone == "" || delivery == ""){
  swal('Заполните пустые поля');
}
else {
// Отсылаем паметры
       $.ajax({
                type: "POST",
                url: "/wp-content/themes/pizza-pucha/send.php",
                data: "name="+name+"&email="+email+"&phone="+phone+"&delivery="+delivery+"&goods="+goods+"&summa="+summa,
                success: function(data) {
                    swal(data);
                    closeCart();
                    window.location.href= '/';
                }
        });
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Александр Одегов, 2016-12-20
@Daatyson

var znachenie = $("div").text();
$.cookie("nazvanie_cookie", znachenie, {expires: 1,path: '/',domain: 'ur-domain.com',});

expires: 1 - время которое хранится кука (в днях если я не ошибаюсь).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question