public static
getProductIdBySku
(mixed $sku, mixed $typeSku = 'sku', mixed $type = array(0, 1))
public static function getProductIdBySku($sku, $typeSku = 'sku', $type = array(0, 1))
{
$db = Factory::getDBO();
$sku = $db->quote($sku);
$typeSkuS = $db->quoteName('a.' . $typeSku);
$typeSkuSA = $db->quoteName('ps.' . $typeSku);
$wheres = array();
$wheresA = array();
$user = PhocacartUser::getUser();
$userLevels = implode(',', $user->getAuthorisedViewLevels());
$userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
$wheres[] = " a.access IN (" . $userLevels . ")";
$wheres[] = " c.access IN (" . $userLevels . ")";
$wheres[] = " (ga.group_id IN (" . $userGroups . ") OR ga.group_id IS NULL)";
$wheres[] = " (gc.group_id IN (" . $userGroups . ") OR gc.group_id IS NULL)";
$wheres[] = " a.published = 1";
$wheres[] = " c.published = 1";
if (!empty($type) && is_array($type)) {
$wheres[] = ' c.type IN (' . implode(',', $type) . ')';
}
$wheresA = $wheres;
$wheres[] = ' ' . $typeSkuS . ' = ' . $sku;
$wheresA[] = ' ' . $typeSkuSA . ' = ' . $sku;
$query = ' SELECT a.id, c.id as catid' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.product_id = a.id' . ' LEFT JOIN #__phocacart_categories AS c ON c.id = pc.category_id' . ' LEFT JOIN #__phocacart_item_groups AS ga ON a.id = ga.item_id AND ga.type = 3' . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 2' . ' WHERE ' . implode(' AND ', $wheres) . ' ORDER BY a.id';
$db->setQuery($query);
$products = $db->loadObjectlist();
if (empty($products) && ($typeSku == 'sku' || $typeSku == 'ean')) {
$query = ' SELECT a.id, c.id AS catid, ps.attributes AS attributes' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.product_id = a.id' . ' LEFT JOIN #__phocacart_categories AS c ON c.id = pc.category_id' . ' LEFT JOIN #__phocacart_product_stock AS ps ON a.id = ps.product_id' . ' LEFT JOIN #__phocacart_item_groups AS ga ON a.id = ga.item_id AND ga.type = 3' . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 2' . ' WHERE ' . implode(' AND ', $wheresA) . ' ORDER BY a.id';
$db->setQuery($query);
$products = $db->loadObjectlist();
}
if (!empty($products)) {
foreach ($products as $k => $v) {
if (isset($v->id) && (int) $v->id > 0 && isset($v->catid) && (int) $v->catid > 0) {
$access = PhocacartProduct::checkIfAccessPossible((int) $v->id, (int) $v->catid, $type);
if ($access) {
$product = array();
$product['id'] = (int) $v->id;
$product['catid'] = (int) $v->catid;
if (isset($v->attributes) && $v->attributes != '') {
$product['attributes'] = unserialize($v->attributes);
}
return $product;
}
}
}
}
return false;
}