getPrimaryKey();
}
return $this->_getTable()->searchBy(array('id' => $cartId), 'transactionId');
}
/**
* Create new shopping cart
*
* @param array $data
* @return mixed
*/
public function create($data = array())
{
return $this->_getTable()->insert($data);
}
/**
* @return null|Zend_Db_Select
*/
public function getListSelect()
{
if (null === $this->_select) {
parent::getListSelect();
$this->_select->join(
$this->_getPair('CartItem'),
'`CartItem`.`cartId` = `' . $this->_tableAlias . '`.`id`',
array(
'*',
'rowTotal' => new Zend_Db_Expr('(`CartItem`.`quantity` * `CartItem`.`price`)'),
'rowTax' => new Zend_Db_Expr('(`CartItem`.`quantity` * `CartItem`.`tax`)')
)
);
$this->_select->joinLeft(
$this->_getPair('CartItemPromo'),
'`CartItemPromo`.`cartId` = `' . $this->_tableAlias . '`.`id` '
. 'AND `CartItem`.`productId` = `CartItemPromo`.`productId`',
array(
'promoValue' => 'value',
'promoType' => 'type',
'promoMinOrderValue' => 'minOrderValue',
)
);
$id = ($id = $this->getPrimaryKey()) ?: '0';
$this->_select->where("`{$this->_tableAlias}`.id = ?", $id, Qs_Db::INT_TYPE);
}
return $this->_select;
}
/**
* Add new item into the shopping cart
*
* @param array $data
* @return int|bool
*/
public function addItem(array $data)
{
unset($data['id'], $data['added'], $data['changed']);
$data['cartId'] = $this->getPrimaryKey();
return $this->_getTable('CartItem')->insert($data);
}
/**
* Return shopping cart items count
*
* @return int
*/
public function getItemsCount()
{
$select = clone $this->getListSelect();
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->columns('SUM(`quantity`)');
return (int) $this->_db->fetchOne($select);
}
/**
* Return items weight
*
* @return float
*/
public function getItemWeight()
{
$select = clone $this->getListSelect();
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->columns('SUM(`weight` * `quantity`)');
return (float) $this->_db->fetchOne($select);
}
/**
* Remove item from shopping cart
*
* @param int $itemId
* @return int
*/
public function removeItem($itemId)
{
$this->_itemData = $this->getItem(array('id' => $itemId));
if ($this->_itemData) {
$itemObj = App_ECommerce_Cart_ItemObjFactory::itemDataFactory($this->_itemData);
// todo: improve code, use transaction
$this->_getTable('CartItemPromo')->delete(array(
'`cartId` = ?' => $this->getPrimaryKey(),
'`productId` = ?' => $this->_itemData['productId'],
));
$result = $this->_getTable('CartItem')->delete(array(
'`id` = ?' => $itemId,
'`cartId` = ?' => $this->getPrimaryKey()
));
$itemObj->afterCartItemRemove();
return $result;
}
return false;
}
/**
* Return cart item
*
* @param array $filter
* @return array|bool
*/
public function getItem($filter)
{
if (!empty($filter)) {
$select = clone $this->getListSelect();
Qs_Db::filter($select, $filter, 'CartItem');
return $this->_db->fetchRow($select);
}
return false;
}
/**
* Return cart items total
* @return float|int
*/
public function getSubtotal()
{
$select = $this->getListSelect();
$list = (array) $this->_db->fetchAll($select);
$subtotal = 0;
foreach ($list as $item) {
$subtotal = Zend_Locale_Math::Add($subtotal, $item['rowTotal'], 4);
}
return $subtotal;
}
/**
* Get total price
*
* @return float|int
*/
public function getTotal()
{
$items = (array) $this->getList();
$total = 0;
foreach ($items as $item) {
$total = Zend_Locale_Math::Add($total, $item['rowTotal'], 4);
}
return $total;
}
public function renderPromoCodeDescription()
{
$text = '';
$data = $this->getData();
if ($data['promoCode']) {
if ($data['promoCodeMinOrderValue'] > $this->getSubtotal()) {
$text .= 'The Promo Code cannot be applied. The order total must be greater than '
. '$' . number_format($data['promoCodeMinOrderValue'], 2);
$text = '' . $text . '.';
} else {
$text .= 'Promo code has been applied. ';
if ($data['promoCodeType'] == App_ECommerce_Promo_Admin_View::PROMO_TYPE_PERCENT) {
require_once 'lib/Smarty/app_plugins/modifier.money.php';
$text .= smarty_modifier_money($data['promoCodeValue'], 2, true) . '% ';
} else {
$text .= '$' . number_format($data['promoCodeValue'], 2, '.', ',') . ' ';
}
$text .= 'off ';
if ($data['promoCodeProduct']) {
$product = $this->getItem(array('productId' => $data['promoCodeProduct']));
if ($product) {
$text .= 'for ' . htmlspecialchars($product['title']) . ' ';
}
}
if ($data['promoCodeMinOrderValue'] > 0) {
$text .= 'for orders over $' . number_format($data['promoCodeMinOrderValue'], 2, '.', ',');
$text .= '';
}
$text = ' ' . $text . '.';
}
}
return $text;
}
public function renderGiftCardDescription()
{
$text = '';
$data = $this->getData();
if ($data['giftCardCode']) {
$giftCardObj = new App_ECommerce_GiftCard_Admin_Obj();
$codeData = $giftCardObj->getDataByCode($data['giftCardCode']);
if (!empty($codeData)) {
if ($codeData['statusId'] == App_ECommerce_GiftCard_Admin_View::CARD_STATUS_REDEEMED) {
$text .= 'The Gift Card already redeemed.';
} else {
$text .= 'Gift Card has been accepted. ';
$text .= '$' . number_format($data['giftCardValue'], 2, '.', ',') . ' OFF!';
$text = ' ' . $text . '';
}
} else {
$text .= 'The Gift Card cannot be applied. Gift Card is not valid.';
}
}
return $text;
}
public function getObjectInfo()
{
$data = parent::getObjectInfo();
if (($itemId = Qs_Request::getRequestValue('itemId'))) {
$item = $this->getItem(array('id' => $itemId));
if (!$item && $this->_itemData) {
$item = $this->_itemData;
}
if ($item) {
$data['itemTitle'] = $item['title'];
} else {
$data['itemTitle'] = 'UNKNOWN ITEM';
}
}
return $data;
}
public function updateItem($cartItemId, array $data)
{
unset($data['id'], $data['added'], $data['changed']);
return $this->_getTable('CartItem')->update($data, array('id = ?' => $cartItemId));
}
public function hasShippableItems()
{
$items = $this->getList();
foreach ($items as $item) {
if ('y' == $item['applyShipping']) {
return true;
}
}
return false;
}
public function isServiceProductInside()
{
$select = $this->_db->select();
$select->from($this->_getPair('CartItem'), new Zend_Db_Expr('1'))
->where('`cartId` = ?', $this->getPrimaryKey(), \Qs_Db::INT_TYPE)
->where('`cartItemType` = ?', Entity::SERVICE_PRODUCT_TYPE)
->limit(1);
return (bool) $this->_db->fetchOne($select);
}
public function removeServiceProducts()
{
$select = $this->_db->select();
$select->from($this->_getPair('CartItem'), 'id')
->where('`cartId` = ?', $this->getPrimaryKey(), \Qs_Db::INT_TYPE)
->where('`cartItemType` = ?', Entity::SERVICE_PRODUCT_TYPE);
$serviceIds = $this->_db->fetchCol($select);
if (!$serviceIds) {
return $this;
}
foreach ($serviceIds as $id) {
$this->removeItem($id);
}
return $this;
}
}