daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Jan 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class CartController extends Controller
  3. {
  4.     public function actionCheckout()
  5.     {
  6.         $this->render('checkout');        
  7.     }
  8.  
  9.     public function actionIndex()
  10.     {
  11.         $session_cart = Yii::app()->user->getState('cart');
  12.         $products = array();
  13.         foreach ($session_cart as $product_id => $quantity) $products[] = $product_id;
  14.         $product_info = Product::model()->findAllByPk($products);
  15.  
  16.         $total_price = 0;
  17.         $cart = array();
  18.         foreach ($product_info as $product) {
  19.             $product_id = $product->product_id;
  20.  
  21.             $cart[$product_id]['product_id'] = $product->product_id;
  22.             $cart[$product_id]['title'] = $product->title;
  23.             $cart[$product_id]['description'] = $product->description;
  24.             $cart[$product_id]['price'] = $product->price;
  25.             $cart[$product_id]['quantity'] = $session_cart[$product_id];
  26.             $cart[$product_id]['total_price'] = round($session_cart[$product_id] * $product->price, 2);
  27.  
  28.             $total_price += $cart[$product_id]['total_price'];
  29.         }
  30.  
  31.         $dataProvider = new CArrayDataProvider($cart, array(
  32.             'id' => 'product_id',
  33.             'sort' => array(
  34.                 'attributes' => array(
  35.                     'id', 'title', 'price',
  36.                 ),
  37.             ),
  38.         ));
  39.  
  40.         $this->render('index',
  41.             array(
  42.                 'dataProvider' => $dataProvider,
  43.                 'total_price' => $total_price
  44.             )
  45.         );
  46.     }
  47.  
  48.     public function actionAdd($product_id, $quantity)
  49.     {
  50.         $product_id = (int)$product_id;
  51.         $quantity = (int)$quantity;
  52.  
  53.         $product_count = Product::model()->exists(
  54.             'product_id=:product_id AND is_available = 1',
  55.             array(':product_id' => $product_id)
  56.         );
  57.  
  58.         if ($product_count) {
  59.             $cart = Yii::app()->user->getState('cart');
  60.             if (isset($cart[$product_id])) {
  61.                 $cart[$product_id] += $quantity;
  62.             } else {
  63.                 $cart[$product_id] = $quantity;
  64.             }
  65.  
  66.             Yii::app()->user->setState('cart', $cart);
  67.         }
  68.  
  69.         $this->redirect(array('cart/index'));
  70.     }
  71.  
  72.     public function actionRemove($product_id)
  73.     {
  74.         $product_id = (int)$product_id;
  75.  
  76.         $cart = Yii::app()->user->getState('cart');
  77.         if (isset($cart[$product_id])) {
  78.             unset($cart[$product_id]);
  79.         }
  80.         Yii::app()->user->setState('cart', $cart);
  81.         $this->redirect(array('cart/index'));
  82.     }
  83.  
  84.     public function actionClear()
  85.     {
  86.         Yii::app()->user->setState('cart', array());
  87.         $this->redirect(array('cart/index'));
  88.     }
  89. }
  90. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top