public function setCoupon($couponId = 0, $couponCode = '')
{
$db = Factory::getDBO();
$user = PhocacartUser::getUser();
$guest = PhocacartUserGuestuser::getGuestUser();
$pos = PhocacartPos::isPos();
// In POS coupons can be set for not selected user (when user is not selected)
$params = PhocacartUtils::getComponentParameters();
$enable_coupons = $params->get('enable_coupons', 2);
$userLevels = implode(',', $user->getAuthorisedViewLevels());
$userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
$wheres = array();
if ((int) $couponId > 0) {
$wheres[] = 'c.id = ' . (int) $couponId;
} else {
if ($couponCode != '') {
$wheres[] = 'c.code = ' . $db->quote((string) $couponCode);
} else {
return false;
}
}
// ACCESS
$wheres[] = " c.access IN (" . $userLevels . ")";
$wheres[] = " (gc.group_id IN (" . $userGroups . ") OR gc.group_id IS NULL)";
$wheres[] = ' c.published = 1';
// COUPON Type
if (!empty($this->type) && is_array($this->type)) {
$wheres[] = " c.type IN (" . implode(',', $this->type) . ')';
}
$where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
// MOVECOUPON
// 1) user logged in ... OK
// 2) guest checkout enabled in options and started by user ... OK
// 3) POS
// 4) $enable_coupons == 1 which means even not logged in user and not started guest checkout user can add coupon
//
// USERS AND COUPONS
// 1) LOGGED IN USER
// 2) USER STARTED GUEST CHECKOUT
// 3) USER NOT LOGGED IN AND NOT STARTED GUEST CHECKOUT YET (PRE-GUEST/PRE-LOGGED-IN)
if (isset($user->id) && $user->id > 0 || $guest || $pos || $enable_coupons == 1) {
$columns = 'c.id, c.code, c.title, c.valid_from, c.valid_to, c.discount,' . ' c.quantity_from, c.available_quantity, c.available_quantity_user, c.total_amount,' . ' c.calculation_type, c.type, c.free_shipping, c.free_payment, c.category_filter, c.product_filter,' . ' co.count AS count, cu.count AS countuser,' . ' GROUP_CONCAT(DISTINCT cp.product_id) AS product,' . ' GROUP_CONCAT(DISTINCT cc.category_id) AS category';
// line of selected categories
$groupsFull = 'c.id, c.code, c.title, c.valid_from, c.valid_to, c.discount,' . ' c.quantity_from, c.available_quantity, c.available_quantity_user, c.total_amount,c.category_filter, c.product_filter,' . ' c.calculation_type, c.type, c.free_shipping, c.free_payment, co.count, cu.count';
//.' co.count AS count, cu.count AS countuser';
$groupsFast = 'c.id';
$groups = PhocacartUtilsSettings::isFullGroupBy() ? $groupsFull : $groupsFast;
$query = 'SELECT ' . $columns . ' FROM #__phocacart_coupons AS c' . ' LEFT JOIN #__phocacart_coupon_products AS cp ON cp.coupon_id = c.id' . ' LEFT JOIN #__phocacart_coupon_categories AS cc ON cc.coupon_id = c.id' . ' LEFT JOIN #__phocacart_coupon_count AS co ON co.coupon_id = c.id' . ' LEFT JOIN #__phocacart_coupon_count_user AS cu ON cu.coupon_id = c.id AND cu.user_id = ' . (int) $user->id . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 6' . $where . ' GROUP BY ' . $groups;
$query .= ' ORDER BY c.id' . ' LIMIT 1';
PhocacartUtils::setConcatCharCount();
$db->setQuery($query);
$coupon = $db->loadAssoc();
if (!empty($coupon)) {
$this->coupon = $coupon;
return true;
} else {
$this->coupon = false;
PhocacartLog::add(4, 'Message - Coupon not valid (Access, Published, Customer Group)', $couponId, 'Coupon code: ' . $couponCode);
return false;
}
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (User not logged in, No guest checkout, Coupons not enabled)', $couponId, 'Coupon code: ' . $couponCode);
return false;
}
}