PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/masportales/application/controllers/cart.php

https://github.com/eloypineda/XHTMLized
PHP | 175 lines | 141 code | 19 blank | 15 comment | 16 complexity | e5e18c7ba85c4bba77c679411cf30e4e MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Cart controller that extends API Controller
  4. *
  5. * This Controller must manage adding, modifing and deleting products to the cart
  6. *
  7. * @since 0.5
  8. *
  9. * @package masPortales
  10. * @subpackage API
  11. */
  12. class Cart extends API_Controller {
  13. private $cart_items;
  14. private $cart_n_items;
  15. private $cart_total;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->load->library('cart');
  20. $this->cart_items = $this->cart->contents();
  21. $this->cart_n_items = $this->cart->total_items();
  22. $this->cart_total = $this->cart->total();
  23. }
  24. public function index()
  25. {
  26. echo "Hello World!";
  27. echo '<pre>';
  28. print_r($this->input->post());
  29. echo '</pre>';
  30. }
  31. public function add()
  32. {
  33. $data = array();
  34. $this->load->model('ecommerce_model');
  35. $product_id = $this->input->post('pid', true);
  36. if ($this->_is_product_in_cart($product_id)) {
  37. $rowid = false;
  38. $message = 'El producto ya ha sido a単adido al carrito.';
  39. } else {
  40. $product_data = $this->ecommerce_model->getProduct($this->input->post('pid'));
  41. $data = array(
  42. 'id' => $product_data['product_ID'],
  43. 'qty' => 1,
  44. 'price' => $product_data['price'],
  45. 'name' => $product_data['name']
  46. );
  47. $rowid = $this->cart->insert($data);
  48. $data['cart_total'] = number_format($this->cart_total + $product_data['price'], 2, ',', '.');
  49. $message = '';
  50. }
  51. header('Content-type: application/json');
  52. echo json_encode(array('rowid' => $rowid, 'rowinfo' => $data, 'message' => $message));
  53. }
  54. public function addqty()
  55. {
  56. $data = array();
  57. $rowid = $this->input->post('rid', true);
  58. if (array_key_exists($rowid, $this->cart_items)) {
  59. $data = array(
  60. 'rowid' => $rowid,
  61. 'qty' => $this->cart_items[$rowid]['qty'] + 1
  62. );
  63. $this->cart->update($data);
  64. $data['price'] = number_format($data['qty'] * $this->cart_items[$rowid]['price'], 2, ',', '.');
  65. $data['cart_total'] = number_format($this->cart_total + $this->cart_items[$rowid]['price'], 2, ',', '.');
  66. $message = 'Ok';
  67. header('Content-type: application/json');
  68. echo json_encode(array('rowid' => $rowid, 'rowinfo' => $data, 'message' => $message));
  69. } else {
  70. $message = 'KO';
  71. $data = array();
  72. header('Content-type: application/json');
  73. echo json_encode(array('rowid' => false, 'rowinfo' => false, 'message' => 'No existe producto en el carrito.'));
  74. }
  75. }
  76. public function subqty()
  77. {
  78. $data = array();
  79. $rowid = $this->input->post('rid', true);
  80. if (array_key_exists($rowid, $this->cart_items)) {
  81. $data = array(
  82. 'rowid' => $rowid,
  83. 'qty' => $this->cart_items[$rowid]['qty'] - 1
  84. );
  85. $this->cart->update($data);
  86. if ($data['qty']) {
  87. $data['price'] = number_format($data['qty'] * $this->cart_items[$rowid]['price'], 2, ',', '.');
  88. $data['cart_total'] = number_format($this->cart_total - $this->cart_items[$rowid]['price'], 2, ',', '.');
  89. $message = 'Ok';
  90. } else {
  91. $data = false;
  92. $message = 'delete';
  93. }
  94. header('Content-type: application/json');
  95. echo json_encode(array('rowid' => $rowid, 'rowinfo' => $data, 'message' => $message));
  96. } else {
  97. $data = array();
  98. header('Content-type: application/json');
  99. echo json_encode(array('rowid' => false, 'rowinfo' => false, 'message' => 'No existe producto en el carrito.'));
  100. }
  101. }
  102. public function setqty()
  103. {
  104. $data = array();
  105. $rowid = $this->input->post('rid', true);
  106. $qty = $this->input->post('qty', true);
  107. //$old_qty = $this->input->post('old_qty', true);
  108. // check if we recieve a real rowid
  109. if (array_key_exists($rowid, $this->cart_items)) {
  110. $old_qty = $this->cart_items[$rowid]['qty'];
  111. // check if it has really been changed the quantity
  112. if ($this->cart_items[$rowid]['qty'] == $qty) {
  113. $data = array();
  114. $rowinfo = false;
  115. $message = 'Quantity not changed';
  116. } else {
  117. $data = array(
  118. 'rowid' => $rowid,
  119. 'qty' => $qty
  120. );
  121. $this->cart->update($data);
  122. $data['difference'] = $qty - $this->cart_items[$rowid]['qty'];
  123. $data['price'] = number_format($data['qty'] * $this->cart_items[$rowid]['price'], 2, ',', '.');
  124. $data['cart_total'] = number_format($this->cart_total + ($this->cart_items[$rowid]['price'] * $data['difference']), 2, ',', '.');
  125. $message = 'Ok';
  126. }
  127. } else {
  128. $data = array();
  129. $rowid = false;
  130. $rowinfo = false;
  131. $message = 'No existe producto en el carrito.';
  132. }
  133. header('Content-type: application/json');
  134. echo json_encode(array('rowid' => $rowid, 'rowinfo' => $data, 'message' => $message));
  135. }
  136. public function delete()
  137. {
  138. $rowid = $this->input->post('rid', true);
  139. $data = array(
  140. 'rowid' => $rowid,
  141. 'qty' => 0
  142. );
  143. $this->cart->update($data);
  144. header('Content-type: application/json');
  145. echo json_encode(array('cart_total' => number_format($this->cart_total - $this->cart_items[$rowid]['price'], 2, ',', '.')));
  146. }
  147. private function _is_product_in_cart($product_id)
  148. {
  149. foreach ($this->cart_items as $rowid => $cart_item) {
  150. if ($cart_item['id'] == $product_id) return $rowid;
  151. }
  152. return false;
  153. }
  154. }
  155. /* End of file cart.php */
  156. /* Location: ./application/controllers/cart.php */