Advertisement
RcPel

cart_functions.php

Oct 29th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. if (isset($_GET['action'])) {
  2.   $action = $_GET['action'];
  3.   $prod   = $_GET['prod_id'];
  4.   $prodname = $_SESSION['product'][$prod];
  5.  
  6.  
  7.  
  8.  
  9.  
  10.   switch ($action) {
  11.     case 'add':
  12.         $result = add_prod($prod, $prodname);
  13.     break;
  14.     case 'plus':
  15.         $result = plus_prod($prod, $prodname);
  16.          
  17.     break;
  18.     case 'remove':
  19.         $result = remove_prod($prod, $prodname);
  20.     break;
  21.     case 'delete':
  22.         $result = delete_prod($prod, $prodname);
  23.     break;
  24.     default:
  25.         $result = ['result'=>'error'];
  26.     break;
  27.   }
  28.  
  29. }
  30.  
  31. $total=0;
  32. $all_products=0;
  33. foreach ($_SESSION['product'] as $prod => $value){
  34. $total_in_cart=count($_SESSION['product']);  
  35.  
  36. }
  37.  
  38. $_SESSION['products']=$total_in_cart;
  39.  
  40. // Calculate subtotal here
  41. $result['totals'] = new stdClass;
  42. $value = $_SESSION['product'][$prod];
  43. $subtotal=$value * $_SESSION['icms'][$prod];
  44.  
  45. $result['subtotal'] =  number_format($value * $_SESSION['icms'][$prod], 2, '.', ''); /* subtotal result here */
  46. $result['cost'] = $_SESSION['icms'][$prod] ;/* Product cost here */
  47. $subtotal= number_format($value *$_SESSION['icms'][$prod], 2, '.', ''); //this session is created in calculates.php
  48. $result['valor_total'] =  $_SESSION['total']= $total +=  $subtotal;
  49. $all_products +=$value;
  50. $_SESSION['total_products']= $all_products;
  51.  
  52. $result['items'] =  $total_in_cart;
  53.  
  54.  
  55. //$result['all_items'] = $_SESSION['total_products'];
  56.  
  57. //$result['items'] = 999;
  58. //$result->items =$_SESSION['products'];
  59.  
  60. echo json_encode($result);
  61.  
  62.  
  63.  
  64. function add_prod($prod, $prodname){
  65.   //add function
  66. $_SESSION['product'][$prod] = 1;
  67. $_SESSION['products']++;
  68. return ['result'=>'success' ];
  69. }
  70. //add +1 function
  71. function plus_prod($prod, $prodname){
  72.   $_SESSION['product'][$prod]++;
  73.   return ['result'=>'success', 'val'=> $_SESSION['product'][$prod]];
  74.  
  75. }
  76. //minus -1 function
  77. function remove_prod($prod, $prodname){
  78.   //sua função para remove
  79. $_SESSION['product'][$prod]--;
  80. $_SESSION['product'][$prod]-1;
  81.   return ['result'=>'success', 'val'=> $_SESSION['product'][$prod]];
  82. }
  83. //delete function
  84. function delete_prod($prod, $prodname){
  85.   //sua função para delete
  86. unset($_SESSION['product'][$prod]);
  87. unset($_SESSION['icms'][$prod]);
  88. $_SESSION['total_products']--;
  89. $_SESSION['products']--;
  90.   return ['result'=>'success'];
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement