Good day!
There is such a code for adding goods to the cart, somehow you can display a notification when you click on add to cart. I tried to set the condition for the button, it does not work, but only 2 buttons are displayed - add to the basket, one leads to the successful addition to the basket, and the other leads to the output of this notification.
<?php if($_SESSION['total_quantity']): ?> <a class="clickMe" href="?view=addtocart&goods_id=<?=$eyestopper['goods_id']?>"><img class="addtocard-index" src="<?=TEMPLATE?>images/addcard-index.jpg" alt="ΠΠΎΠ±Π°Π²ΠΈΡΡ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ" /></a> <div class="overlay" title="ΠΎΠΊΠ½ΠΎ"></div> <div class="popup"> <div class="close_order">x</div> <p>Π’ΠΎΠ²Π°Ρ ΡΡΠΏΠ΅ΡΠ½ΠΎ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ</p> </div> <?php endif; ?>
I provide below code:
session_start(); /* ===Π‘ΡΠΌΠΌΠ° Π·Π°ΠΊΠ°Π·Π° Π² ΠΊΠΎΡΠ·ΠΈΠ½Π΅ + Π°ΡΡΠΈΠ±ΡΡΡ ΡΠΎΠ²Π°ΡΠ°===*/ function total_sum($goods, $connection){ $total_sum = 0; $str_goods = implode(',',array_keys($goods)); $query = "SELECT goods_id, name, price FROM goods WHERE goods_id IN ($str_goods)"; $res = mysqli_query($connection, $query) or die(mysqli_error()); while($row = mysqli_fetch_assoc($res)){ $_SESSION['cart'][$row['goods_id']]['name'] = $row['name']; $_SESSION['cart'][$row['goods_id']]['price'] = $row['price']; $total_sum += $_SESSION['cart'][$row['goods_id']]['qty'] * $row['price']; } return $total_sum; } /* ===Π‘ΡΠΌΠΌΠ° Π·Π°ΠΊΠ°Π·Π° Π² ΠΊΠΎΡΠ·ΠΈΠ½Π΅ + Π°ΡΡΠΈΠ±ΡΡΡ ΡΠΎΠ²Π°ΡΠ°===*/ case('addtocart'): // Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π² ΠΊΠΎΡΠ·ΠΈΠ½Ρ $goods_id = abs((int)$_GET['goods_id']); addtocart($goods_id); $_SESSION['total_sum'] = total_sum($_SESSION['cart'], $connection); // ΠΊΠΎΠ»-Π²ΠΎ ΡΠΎΠ²Π°ΡΠ° Π² ΠΊΠΎΡΠ·ΠΈΠ½Π΅ + Π·Π°ΡΠΈΡΠ° ΠΎΡ Π²Π²ΠΎΠ΄Π° Π½Π΅ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠ΅Π³ΠΎ ID ΡΠΎΠ²Π°ΡΠ° $_SESSION['total_quantity'] = 0; foreach($_SESSION['cart'] as $key => $value){ if(isset($value['price'])){ // Π΅ΡΠ»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½Π° ΡΠ΅Π½Π° ΡΠΎΠ²Π°ΡΠ° ΠΈΠ· ΠΠ - ΡΡΠΌΠΌΠΈΡΡΠ΅ΠΌ ΠΊΠΎΠ»-Π²ΠΎ $_SESSION['total_quantity'] += $value['qty']; }else{ // ΠΈΠ½Π°ΡΠ΅ - ΡΠ΄Π°Π»ΡΠ΅ΠΌ ΡΠ°ΠΊΠΎΠΉ ID ΠΈΠ· ΡΠ΅ΡΡΠΈΠΈΠΈ (ΠΊΠΎΡΠ·ΠΈΠ½Ρ) unset($_SESSION['cart'][$key]); } } redirect(); break; <h2><span>ΠΠΎΡΠ·ΠΈΠ½Π°</span></h2> <div> <p class="onK"> ?php if($_SESSION['total_quantity']): ?> Π’ΠΎΠ²Π°ΡΠΎΠ² Π² ΠΊΠΎΡΠ·ΠΈΠ½Π΅:<br /> <span><?=$_SESSION['total_quantity']?></span> Π½Π° ΡΡΠΌΠΌΡ <span><?=$_SESSION['total_sum']?></span> ΡΡΠ±. <img src="<?=TEMPLATE?>images/korzinaON.png" class="korzinaON" /> <a href="#"><img src="<?=TEMPLATE?>images/oformit.png" class="oformit" alt="ΠΡΠΎΡΠΌΠΈΡΡ Π·Π°ΠΊΠ°Π·" /></a> <?php else: ?> <span class="offK">ΠΠΎΡΠ·ΠΈΠ½Π° ΠΏΡΡΡΠ°</span> <img src="<?=TEMPLATE?>images/korzinaOFF.png" class="korzinaOFF" /> <?php endif; ?> </p> </div>
And connected:
<script type="text/javascript" src="js/jquery.cookie.js"></script> <script type="text/javascript" src="js/workscripts.js"></script> <script type="text/javascript"> var query = '<?=$_SERVER['QUERY_STRING']?>';</script>
Self notification:
<a class="clickMe"href="#">ΠΡΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π²ΡΡΠ»ΠΎ ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅</a>
Function:
var popup = $('.popup'), overlay = $('.overlay'), timer; overlay.add($('.close_order',popup)).click(hidePopup); $('a.clickMe').click(function (e){ e.preventDefault(); popup.add(overlay).finish().fadeIn(function(){ clearTimeout(timer); timer = setTimeout(hidePopup, 3000); }); }); function hidePopup(){ return popup.add(overlay).fadeOut(); }
jQuery.cookie:
jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } };
And how to remove it when updating?