Back to PhocacartAttribute class

Method storeCombinationsById

public static
storeCombinationsById
(mixed $productId, mixed $aosArray, mixed $new = 0)

Method storeCombinationsById - Source code

public static function storeCombinationsById($productId, $aosArray, $new = 0)
{
    if ((int) $productId > 0) {
        $db = Factory::getDBO();
        $notDeleteItems = array();
        if (!empty($aosArray)) {
            $values = array();
            foreach ($aosArray as $k => $v) {
                if (!is_array($v)) {
                    $v = array();
                }
                // correct simple xml
                if (!isset($v['product_id'])) {
                    $v['product_id'] = '';
                }
                if (!isset($v['product_key'])) {
                    $v['product_key'] = '';
                }
                if (!isset($v['attributes'])) {
                    $v['attributes'] = '';
                }
                if (!isset($v['stock'])) {
                    $v['stock'] = '';
                }
                if (empty($v['product_id'])) {
                    $v['product_id'] = '';
                }
                if (empty($v['product_key'])) {
                    $v['product_key'] = '';
                }
                if (empty($v['attributes'])) {
                    $v['attributes'] = '';
                }
                if (empty($v['stock'])) {
                    $v['stock'] = '';
                }
                if (empty($v['price'])) {
                    $v['price'] = '';
                }
                if (empty($v['sku'])) {
                    $v['sku'] = '';
                }
                if (empty($v['ean'])) {
                    $v['ean'] = '';
                }
                if (empty($v['image'])) {
                    $v['image'] = '';
                }
                if ($v['product_key'] == '') {
                    continue;
                }
                $idExists = 0;
                if ($new == 0) {
                    if (isset($v['id']) && $v['id'] > 0) {
                        // Does the row exist
                        $query = ' SELECT id ' . ' FROM #__phocacart_product_stock' . ' WHERE id = ' . (int) $v['id'] . ' ORDER BY id';
                        $db->setQuery($query);
                        $idExists = $db->loadResult();
                    }
                }
                if ((int) $idExists > 0) {
                    $query = 'UPDATE #__phocacart_product_stock SET' . ' product_id = ' . (int) $productId . ',' . ' product_key = ' . $db->quote($v['product_key']) . ',' . ' stock = ' . (int) $v['stock'] . ',' . ' price = ' . $db->quote($v['price']) . ',' . ' sku = ' . $db->quote($v['sku']) . ',' . ' ean = ' . $db->quote($v['ean']) . ',' . ' image = ' . $db->quote($v['image']) . ',' . ' WHERE id = ' . (int) $idExists;
                    $db->setQuery($query);
                    $db->execute();
                    $newIdD = $idExists;
                } else {
                    $values = '(' . (int) $productId . ', ' . $db->quote($v['product_key']) . ', ' . $db->quote($v['attributes']) . ', ' . (int) $v['stock'] . ', ' . (int) $v['price'] . ', ' . (int) $v['sku'] . ', ' . (int) $v['ean'] . ', ' . $db->quote($v['image']) . ')';
                    $query = ' INSERT INTO #__phocacart_product_stock (product_id, product_key, attributes, stock, price, sku, ean, image)' . ' VALUES ' . $values;
                    $db->setQuery($query);
                    $db->execute();
                    $newIdD = $db->insertid();
                }
                $notDeleteItems[] = $newIdD;
            }
        }
        // Remove all discounts except the active
        if (!empty($notDeleteItems)) {
            $notDeleteItemsString = implode(',', $notDeleteItems);
            $query = ' DELETE ' . ' FROM #__phocacart_product_stock' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteItemsString . ')';
        } else {
            $query = ' DELETE ' . ' FROM #__phocacart_product_stock' . ' WHERE product_id = ' . (int) $productId;
        }
        $db->setQuery($query);
        $db->execute();
    }
}