Advertisement
Guest User

shopFrontendCartAdd.controller.php

a guest
Jul 7th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.01 KB | None | 0 0
  1. <?php
  2.  
  3. class shopFrontendCartAddController extends waJsonController
  4. {
  5.     /**
  6.      * @var shopCart
  7.      */
  8.     protected $cart;
  9.  
  10.     /**
  11.      * @var shopCartItemsModel
  12.      */
  13.     protected $cart_model;
  14.     /**
  15.      * @var bool
  16.      */
  17.     protected $is_html;
  18.     /**
  19.      * @var bool
  20.      */
  21.     protected $get_full;
  22.  
  23.     public function execute()
  24.     {
  25.         $code = waRequest::cookie('shop_cart');
  26.         if (!$code) {
  27.             $code = md5(uniqid(time(), true));
  28.             // header for IE
  29.             wa()->getResponse()->addHeader('P3P', 'CP="NOI ADM DEV COM NAV OUR STP"');
  30.             // set cart cookie
  31.             wa()->getResponse()->setCookie('shop_cart', $code, time() + 30 * 86400, null, '', false, true);
  32.         }
  33.         $this->cart = new shopCart($code);
  34.         $this->cart_model = new shopCartItemsModel();
  35.  
  36.         $data = waRequest::post();
  37.         $this->is_html = waRequest::request('html');
  38.         $this->get_full = waRequest::request('full');
  39.  
  40.         // add service
  41.         if (isset($data['parent_id'])) {
  42.             $this->addService($data);
  43.             return;
  44.         }
  45.  
  46.         // add sku
  47.         $sku_model = new shopProductSkusModel();
  48.         $product_model = new shopProductModel();
  49.         if (!isset($data['product_id'])) {
  50.             $sku = $sku_model->getById($data['sku_id']);
  51.             $product = $product_model->getById($sku['product_id']);
  52.         } else {
  53.             $product = $product_model->getById($data['product_id']);
  54.             if (isset($data['sku_id'])) {
  55.                 $sku = $sku_model->getById($data['sku_id']);
  56.             } else {
  57.                 if (isset($data['features'])) {
  58.                     $product_features_model = new shopProductFeaturesModel();
  59.                     $sku_id = $product_features_model->getSkuByFeatures($product['id'], $data['features']);
  60.                     if ($sku_id) {
  61.                         $sku = $sku_model->getById($sku_id);
  62.                     } else {
  63.                         $sku = null;
  64.                     }
  65.                 } else {
  66.                     $sku = $sku_model->getById($product['sku_id']);
  67.                     if (!$sku['available']) {
  68.                         $sku = $sku_model->getByField(array('product_id' => $product['id'], 'available' => 1));
  69.                     }
  70.  
  71.                     if (!$sku) {
  72.                         $this->errors = _w('This product is not available for purchase');
  73.                         return;
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
  79.         $quantity = waRequest::post('quantity', 1);
  80.         if ($product && $sku) {
  81.             // check quantity
  82.             if (!wa()->getSetting('ignore_stock_count')) {
  83.                 $c = $this->cart_model->countSku($code, $sku['id']);
  84.                 if ($sku['count'] !== null && $c + $quantity > $sku['count']) {
  85.                     $quantity = $sku['count'] - $c;
  86.                     $name = $product['name'].($sku['name'] ? ' ('.$sku['name'].')' : '');
  87.                     if (!$quantity) {
  88.                         if ($sku['count'] > 0)
  89.                             $this->errors = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name);
  90.                         else
  91.                             $this->errors = sprintf(_w('Oops! %s just went out of stock and is not available for purchase at the moment. We apologize for the inconvenience.'), $name);
  92.                         return;
  93.                     } else {
  94.                         $this->response['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name);
  95.                     }
  96.                 }
  97.             }
  98.             $services = waRequest::post('services', array());
  99.             if ($services) {
  100.                 $variants = waRequest::post('service_variant');
  101.                 $temp = array();
  102.                 $service_ids = array();
  103.                 foreach ($services as $service_id) {
  104.                     if (isset($variants[$service_id])) {
  105.                         $temp[$service_id] = $variants[$service_id];
  106.                     } else {
  107.                         $service_ids[] = $service_id;
  108.                     }
  109.                 }
  110.                 if ($service_ids) {
  111.                     $service_model = new shopServiceModel();
  112.                     $temp_services = $service_model->getById($service_ids);
  113.                     foreach ($temp_services as $row) {
  114.                         $temp[$row['id']] = $row['variant_id'];
  115.                     }
  116.                 }
  117.                 $services = $temp;
  118.             }
  119.             $item_id = null;
  120.             $item = $this->cart_model->getItemByProductAndServices($code, $product['id'], $sku['id'], $services);
  121.             if ($item) {
  122.                 $item_id = $item['id'];
  123.                 $this->cart->setQuantity($item_id, $item['quantity'] + $quantity);
  124.             }
  125.             if (!$item_id) {
  126.                 $data = array(
  127.                     'create_datetime' => date('Y-m-d H:i:s'),
  128.                     'product_id' => $product['id'],
  129.                     'sku_id' => $sku['id'],
  130.                     'quantity' => $quantity,
  131.                     'type' => 'product'
  132.                 );
  133.                 if ($services) {
  134.                     $data_services = array();
  135.                     foreach ($services as $service_id => $variant_id) {
  136.                         $data_services[] = array(
  137.                             'service_id' => $service_id,
  138.                             'service_variant_id' => $variant_id,
  139.                         );
  140.                     }
  141.                 } else {
  142.                     $data_services = array();
  143.                 }
  144.                 $item_id = $this->cart->addItem($data, $data_services);
  145.             }
  146.             if (waRequest::isXMLHttpRequest()) {
  147.                 $discount = $this->cart->discount($order);
  148.                 if (!empty($order['params']['affiliate_bonus'])) {
  149.                     $discount -= shop_currency(shopAffiliate::convertBonus($order['params']['affiliate_bonus']), $this->getConfig()->getCurrency(true), null, false);
  150.                 }
  151.                 $this->response['item_id'] = $item_id;
  152.                 $this->response['total'] = $this->currencyFormat($this->cart->total());
  153.                 $this->response['discount'] = $this->currencyFormat($discount);
  154.                 $this->response['discount_coupon'] = $this->currencyFormat(ifset($order['params']['coupon_discount'], 0), true);
  155.                 $this->response['count'] = $this->cart->count();
  156.                 if($this->get_full) {
  157.                     $cart_items = $this->cart->items();
  158.                     foreach($cart_items as $item) {
  159.                         $cart_items[$item['id']]['product']['thumb'] = shopImage::getUrl(array(
  160.                             'product_id' => $item['product_id'],
  161.                             'id' => $item['product']['image_id'],
  162.                             'ext' => $item['product']['ext']
  163.                         ), "48x48");
  164.                         $cart_items[$item['id']]['product']['frontend_url'] = "";
  165.                     }
  166.                     $this->response['items'] = $cart_items;
  167.                 }
  168.             } else {
  169.                 $this->redirect(waRequest::server('HTTP_REFERER'));
  170.             }
  171.         } else {
  172.             throw new waException('product not found');
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * @param float $val
  178.      * @param string|bool $currency
  179.      * @return string
  180.      */
  181.     protected function currencyFormat($val, $currency = true)
  182.     {
  183.         return $this->is_html ? shop_currency_html($val, $currency) : shop_currency($val, $currency);
  184.     }
  185.  
  186.     /**
  187.      * @param $data
  188.      */
  189.     protected function addService($data)
  190.     {
  191.         $item = $this->cart_model->getById($data['parent_id']);
  192.         if (!$item) {
  193.             $this->errors = _w('Error');
  194.             return;
  195.         }
  196.         unset($item['id']);
  197.         $item['parent_id'] = $data['parent_id'];
  198.         $item['type'] = 'service';
  199.         $item['service_id'] = $data['service_id'];
  200.         if (isset($data['service_variant_id'])) {
  201.             $item['service_variant_id'] = $data['service_variant_id'];
  202.         } else {
  203.             $service_model = new shopServiceModel();
  204.             $service = $service_model->getById($data['service_id']);
  205.             $item['service_variant_id'] = $service['variant_id'];
  206.         }
  207.  
  208.         if ($row = $this->cart_model->getByField(array('parent_id' => $data['parent_id'], 'service_variant_id' => $item['service_variant_id']))) {
  209.             $id = $row['id'];
  210.         } else {
  211.             $id = $this->cart->addItem($item);
  212.         }
  213.         $total = $this->cart->total();
  214.         $discount = $this->cart->discount($order);
  215.         if (!empty($order['params']['affiliate_bonus'])) {
  216.             $discount -= shop_currency(shopAffiliate::convertBonus($order['params']['affiliate_bonus']), $this->getConfig()->getCurrency(true), null, false);
  217.         }
  218.  
  219.         $this->response['id'] = $id;
  220.         $this->response['total'] = $this->currencyFormat($total);
  221.         $this->response['count'] = $this->cart->count();
  222.         $this->response['discount'] = $this->currencyFormat($discount);
  223.         $this->response['discount_coupon'] = $this->currencyFormat(ifset($order['params']['coupon_discount'], 0), true);
  224.  
  225.         $item_total = $this->cart->getItemTotal($data['parent_id']);
  226.         $this->response['item_total'] = $this->currencyFormat($item_total);
  227.  
  228.         if (shopAffiliate::isEnabled()) {
  229.             $add_affiliate_bonus = shopAffiliate::calculateBonus(array(
  230.                 'total' => $total,
  231.                 'discount' => $discount,
  232.                 'items' => $this->cart->items(false)
  233.             ));
  234.             $this->response['add_affiliate_bonus'] = sprintf(
  235.                 _w("This order will add +%s points to your affiliate bonus."),
  236.                 round($add_affiliate_bonus, 2)
  237.             );
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement