public static
storeProductPriceGroupsById
(mixed $data, mixed $productId)
public static function storeProductPriceGroupsById($data, $productId)
{
if (!empty($data)) {
$app = Factory::getApplication();
$db = Factory::getDBO();
$notDeleteItems = array();
foreach ($data as $k => $v) {
if (!isset($v['price']) || isset($v['price']) && $v['price'] == '') {
// Price is not 0, price is empty
// We can set price to zero so we need to differentiate between zero and not set
continue;
}
//$row = JTable::getInstance('PhocacartProductPriceGroup', 'Table', array());
$idExists = 0;
if (isset($v['product_id']) && $v['product_id'] != '' && isset($v['group_id']) && $v['group_id'] != '') {
$query = ' SELECT id' . ' FROM #__phocacart_product_price_groups' . ' WHERE product_id = ' . (int) $v['product_id'] . ' AND group_id = ' . (int) $v['group_id'] . ' ORDER BY id';
$db->setQuery($query);
$idExists = $db->loadResult();
}
if ((int) $idExists > 0) {
$query = 'UPDATE #__phocacart_product_price_groups SET' . ' product_id = ' . (int) $v['product_id'] . ',' . ' group_id = ' . (int) $v['group_id'] . ',' . ' price = ' . $db->quote($v['price']) . ' WHERE id = ' . (int) $idExists;
$db->setQuery($query);
$db->execute();
$newIdD = $idExists;
} else {
if (isset($v['id']) && (int) $v['id'] > 0) {
// IMPORT
$values = '(' . (int) $v['id'] . ', ' . (int) $v['product_id'] . ', ' . (int) $v['group_id'] . ', ' . $db->quote($v['price']) . ')';
$query = ' INSERT INTO #__phocacart_product_price_groups (id, product_id, group_id, price) VALUES ' . $values;
$db->setQuery($query);
$db->execute();
$newIdD = (int) $v['id'];
} else {
// NEW ITEM
$values = '(' . (int) $v['product_id'] . ', ' . (int) $v['group_id'] . ', ' . $db->quote($v['price']) . ')';
$query = ' INSERT INTO #__phocacart_product_price_groups (product_id, group_id, price) VALUES ' . $values;
$db->setQuery($query);
$db->execute();
$newIdD = $db->insertid();
}
}
if (isset($newIdD) && (int) $newIdD > 0) {
$notDeleteItems[] = (int) $newIdD;
}
}
if (!empty($notDeleteItems)) {
$notDeleteItemsString = implode(',', $notDeleteItems);
$query = ' DELETE ' . ' FROM #__phocacart_product_price_groups' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteItemsString . ')';
} else {
$query = ' DELETE ' . ' FROM #__phocacart_product_price_groups' . ' WHERE product_id = ' . (int) $productId;
}
$db->setQuery($query);
$db->execute();
}
return true;
}