Back to PhocacartGroup class

Method storeGroupsById

public static
storeGroupsById
(mixed $id, mixed $type, mixed $groups, mixed $productId = 0)

Method storeGroupsById - Source code

/*
 * Product ID is only used by product discounts - to successfully clean the table we need info about
 * in which product the product discount is used
 */
public static function storeGroupsById($id, $type, $groups, $productId = 0)
{
    if ((int) $id > 0) {
        $db = Factory::getDBO();
        $query = ' DELETE ' . ' FROM #__phocacart_item_groups' . ' WHERE item_id = ' . (int) $id;
        if ($productId > 0) {
            $query .= ' AND product_id = ' . (int) $productId;
        }
        $query .= ' AND type = ' . (int) $type;
        $db->setQuery($query);
        $db->execute();
        if (!empty($groups)) {
            $values = array();
            $activeGroups = array();
            foreach ($groups as $k => $v) {
                $values[] = ' (' . (int) $id . ', ' . (int) $v . ', ' . (int) $productId . ', ' . (int) $type . ')';
                $activeGroups[] = (int) $v;
            }
            if (!empty($values)) {
                $valuesString = implode(',', $values);
                $query = ' INSERT INTO #__phocacart_item_groups (item_id, group_id, product_id, type)' . ' VALUES ' . (string) $valuesString;
                $db->setQuery($query);
                $db->execute();
            }
            // Product groups have two tables assinged
            // phocacart_product_price_groups
            // phocacart_product_point_groups
            if (!empty($activeGroups) && $type == 3) {
                $activeGroupsS = implode(',', $activeGroups);
                $q1 = 'DELETE FROM #__phocacart_product_price_groups' . ' WHERE product_id = ' . (int) $id . ' AND group_id NOT IN ( ' . $activeGroupsS . ' )';
                $db->setQuery($q1);
                $db->execute();
                $q1 = 'DELETE FROM #__phocacart_product_point_groups' . ' WHERE product_id = ' . (int) $id . ' AND group_id NOT IN ( ' . $activeGroupsS . ' )';
                $db->setQuery($q1);
                $db->execute();
            }
        }
    }
}