_getDataObj()->setPrimaryKey($this->_getDataObj()->getCartId()); parent::__construct($options); } public function exec() { if ($this->getConfig('type') == self::TYPE_PREVIEW) { $this->setPreviewMode(); $this->_getLog()->addIgnoredControllers(array('App_ECommerce_Cart_View' => array('list'))); } else { if (!($this->_doc instanceof App_Doc_Admin)) { $this->_getDataObj()->removeServiceProducts(); } } return parent::exec(); } protected function _addListItem() { $this->_initListRedirection(); $item = $this->_getListItem(); if (false === $this->getPreviewMode()) { $this->_doc->addScript('js/app/ECommerce/cart.js'); $this->_doc->addScript('js/jquery-autoNumeric.js'); $params = array( 'qtyList' => $this->_getQtyList() ); $this->_doc->addInitFunction('App_Cart_Cart.init', array($params)); $this->_doc->addGtmValues(Qs_Gtm::adGetShoppingCart($item['list'])); if (Qs_Doc::ANALYTICS_TYPE_GOOGLE_TAG_MANAGER == $this->_doc->getAnalyticsType()) { $cartItems = Qs_Gtm::eeGetProducts($this->_getDataObj()->getList(true), Qs_Gtm::DATASET_CART, [], true); $options = ['cartItems' => $cartItems]; $this->_doc->addInitObject('lib.Gtm', [$options]); } } $this->_addLinksItem(); $this->_addItem($item); return $this; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['previewMode'] = $this->getPreviewMode(); return $options; } protected function _getQtyList() { $items = (array) $this->_getListItem(); $qtyList = array(); foreach ($items['list'] as $item) { $qtyList[$item['id']] = $item['quantity']; } return $qtyList; } /** * Get list item. Add cart summary info. * * @internal param null $list * @return array|null */ protected function _getListItem() { $list = parent::_getListItem(); $list['shoppingCartSummary'] = $this->_getDataObj()->getTotal(); $list['previewMode'] = $this->getPreviewMode(); $this->_getDataObj()->validateCartItems(); $list['cartErrors'] = $this->_getDataObj()->getErrors(); return $list; } /** * Update cart item * Display 404 page when request is not ajax */ protected function _doUpdateItem() { return $this->_doc->display404(); } /** * Ajax. Update cart item */ protected function _doUpdateItemAjax() { $result = false; $itemId = Qs_Request::getRequestValue('itemId'); $qty = Qs_Request::getRequestValue('qty'); if (!empty($itemId)) { if (!$qty) { $qty = 1; } $result = $this->_getDataObj()->updateItemQty($itemId, $qty); } $response = array(); $response['success'] = $result; if ($result) { $row = $this->_getDataObj()->getRowData($itemId); $currency = static::CURRENCY_SYMBOL; $response['rowPrice'] = $currency . number_format($row['price'], 2); $response['rowTotal'] = $currency . number_format($row['rowTotal'], 2); if (!empty($row['promoDescription'])) { $response['rowTotal'] .= ' (' . htmlspecialchars($row['promoDescription']) . ')'; } $subtotal = 0; if ($this->_getDataObj()->getData('transactionId')) { $orderObj = new App_ECommerce_Order_Obj(); $orderObj->setPrimaryKey($this->_getDataObj()->getData('transactionId')); $subtotal = $orderObj->getData('subtotal'); } if ($subtotal <= 0) { $subtotal = $this->_getDataObj()->getTotal(); } $response['subTotal'] = $currency . number_format(Zend_Locale_Math::round($subtotal, 2), 2); $response['quantity'] = $this->_getDataObj()->getItemsCount(); $this->_log(); } $this->_displayJson($response); } /** * Clear default links * * @return array */ protected function _getDefaultLinks() { return array(); } /** * Get cart list HTML for JSONP requests */ protected function _doGetListHtml() { $this->_doGetListHtmlAjax(); } /** * Ajax. Get cart list HTML */ protected function _doGetListHtmlAjax() { $item = $this->_getListItem(); Qs_Smarty_File::setTemplate('ECommerce/Cart/small-list.tpl'); $data = array( 'item' => array( 'list' => $item['list'], 'subTotal' => $item['shoppingCartSummary'], 'config' => $item['config'], ) ); $list = Qs_Smarty_File::render($data); $response = array('success' => true, 'html' => $list); $callback = Qs_Array::get($_GET, 'callback'); $this->_displayJson($response, $callback); } /** * Set transaction id * * @param $id * @return bool|Exception */ public function setTransactionId($id) { if (($e = $this->_getDataObj()->updateTransactionId($id)) instanceof Exception) { return $e; } return true; } /** * Clean transactionId field * * @param int $id * @return bool|Exception */ public function cancelTransaction($id) { if (($e = $this->_getDataObj()->cancelTransaction($id)) instanceof Exception) { return $e; } return true; } public function getItemsCount() { return $this->_getDataObj()->getItemsCount(); } /** * Get GTM Products for JSONP requests */ protected function _doGetGtmProducts() { $this->_doGetGtmProductsAjax(); } /** * Ajax. Get GTM Products */ protected function _doGetGtmProductsAjax() { $products = App_ECommerce_Cart_Obj::getInstance()->getList(); $products = Qs_Gtm::eeGetProducts($products, Qs_Gtm::DATASET_CHECKOUT); $response = [ 'success' => true, 'products' => $products, ]; $callback = Qs_Array::get($_GET, 'callback'); $this->_displayJson($response, $callback); } }