コード例 #1
0
/**
 * Add to cart product with options
 *
 * @param int $productID
 * @param array $variants  - row is variantID
 * @param int $qty
 */
function cartAddToCart($productID, $variants, $qty = 1, $sample = 0)
{
    if ($qty === '') {
        $qty = 1;
    }
    $qty = max(0, intval($qty));
    $productID = intval($productID);
    $product_data = GetProduct($productID);
    if (!$product_data['ordering_available']) {
        return false;
    }
    if (!$product_data['enabled']) {
        return false;
    }
    $is = intval($product_data['in_stock']);
    $min_order_amount = $product_data['min_order_amount'];
    //$min_order_amount = db_phquery_fetch(DBRFETCH_FIRST, "SELECT min_order_amount FROM ?#PRODUCTS_TABLE WHERE productID=?", $productID );
    if (!isset($_SESSION["log"])) {
        //save shopping cart in the session variables
        //$_SESSION["gids"] contains product IDs
        //$_SESSION["counts"] contains product quantities
        //($_SESSION["counts"][$i] corresponds to $_SESSION["gids"][$i])
        //$_SESSION["configurations"] contains variants
        //$_SESSION[gids][$i] == 0 means $i-element is 'empty'
        if (!isset($_SESSION["gids"])) {
            $_SESSION["gids"] = array();
            $_SESSION["counts"] = array();
            $_SESSION["configurations"] = array();
            $_SESSION["sample"] = array();
        }
        //check for current item in the current shopping cart content
        $item_index = SearchConfigurationInSessionVariable($variants, $productID);
        if ($item_index != -1) {
            //increase current product's quantity
            /*if($_SESSION["counts"][$item_index]+$qty<$min_order_amount){
            	 $qty=$min_order_amount-$_SESSION["counts"][$item_index];
            	 }*/
            //$qty = max($qty,$min_order_amount - $_SESSION["counts"][$item_index],0);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is - $_SESSION["counts"][$item_index]);
            }
            $qty = max($qty, 0);
            $_SESSION["sample"][$item_index] = $sample;
            if (CONF_CHECKSTOCK == 0 || $_SESSION["counts"][$item_index] + $qty <= $is && $is && $qty) {
                if ($sample) {
                    $_SESSION["counts"][$item_index] = 1;
                } else {
                    $_SESSION["counts"][$item_index] += $qty;
                }
            } else {
                return $_SESSION["counts"][$item_index];
            }
        } else {
            //no item - add it to $gids array
            $qty = max($qty, $min_order_amount, 0);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is);
            }
            $qty = max($qty, 0);
            if ($sample) {
                $qty = 1;
            }
            $_SESSION["sample"][] = $sample;
            if (CONF_CHECKSTOCK == 0 || $is >= $qty && $qty) {
                $_SESSION["gids"][] = $productID;
                $_SESSION["counts"][] = $qty;
                $_SESSION["configurations"][] = $variants;
                cartUpdateAddCounter($productID);
            } else {
                return 0;
            }
        }
    } else {
        //authorized customer - get cart from database
        $itemID = SearchConfigurationInDataBase($variants, $productID);
        $customerEntry = Customer::getAuthedInstance();
        if (is_null($customerEntry)) {
            return false;
        }
        if ($itemID != -1) {
            // if this configuration exists in database
            $quantity = db_phquery_fetch(DBRFETCH_FIRST, "SELECT Quantity FROM ?#SHOPPING_CARTS_TABLE WHERE customerID=? AND itemID=?", $customerEntry->customerID, $itemID);
            /*if($quantity+$qty<$min_order_amount){
            	 $qty=$min_order_amount-$quantity;
            	 }*/
            //$qty = max($qty,$min_order_amount - $quantity);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is - $quantity);
            }
            $qty = max($qty, 0);
            if (CONF_CHECKSTOCK == 0 || $quantity + $qty <= $is && $is) {
                if ($sample) {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", 1, $sample, $customerEntry->customerID, $itemID);
                } else {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", $quantity + $qty, $sample, $customerEntry->customerID, $itemID);
                }
            } else {
                return $quantity;
            }
        } else {
            //insert new item
            $qty = max($qty, $min_order_amount);
            if (CONF_CHECKSTOCK != 0 && $qty > $is) {
                $qty = min($qty, $is);
            }
            if ($sample) {
                $qty = 1;
            }
            if ((CONF_CHECKSTOCK == 0 || $is >= $qty) && $qty > 0) {
                $itemID = InsertNewItem($variants, $productID);
                InsertItemIntoCart($itemID);
                if ($sample) {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", 1, $sample, $customerEntry->customerID, $itemID);
                } else {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", $qty, $sample, $customerEntry->customerID, $itemID);
                }
                cartUpdateAddCounter($productID);
            } else {
                return 0;
            }
        }
    }
    //db_phquery("UPDATE ?#PRODUCTS_TABLE SET add2cart_counter=(add2cart_counter+1) WHERE productID=?",$productID);
    return true;
}
コード例 #2
0
ファイル: cart_functions.php プロジェクト: gblok/rsc
function cartAddToCart($productID, $variants)
{
    $is = GetProductInStockCount($productID);
    $sql = '
        SELECT
        min_order_amount
        FROM ' . PRODUCTS_TABLE . '
        WHERE productID=' . (int) $productID;
    $q = db_query($sql);
    $min_order_amount = db_fetch_row($q);
    $min_order_amount = $min_order_amount[0];
    $count_to_order = 1;
    if (!isset($_SESSION['log'])) {
        //save shopping cart in the session variables
        //$_SESSION['gids'] contains product IDs
        //$_SESSION['counts'] contains product quantities
        //                        ($_SESSION['counts'][$i] corresponds to $_SESSION['gids'][$i])
        //$_SESSION['configurations'] contains variants
        //$_SESSION[gids][$i] == 0 means $i-element is 'empty'
        if (!isset($_SESSION['gids'])) {
            $_SESSION['gids'] = array();
            $_SESSION['counts'] = array();
            $_SESSION['configurations'] = array();
        }
        //check for current item in the current shopping cart content
        $item_index = SearchConfigurationInSessionVariable($variants, $productID);
        if ($item_index == -1) {
            $count_to_order = $min_order_amount;
        }
        if ($item_index != -1) {
            //increase current product's quantity
            if (CONF_CHECKSTOCK == 0 || $_SESSION['counts'][$item_index] + $count_to_order <= $is) {
                $_SESSION['counts'][$item_index] += $count_to_order;
            } else {
                return false;
            }
        } else {
            if (CONF_CHECKSTOCK == 0 || $is >= $count_to_order) {
                //no item - add it to $gids array
                $_SESSION['gids'][] = $productID;
                $_SESSION['counts'][] = $count_to_order;
                $_SESSION['configurations'][] = $variants;
            } else {
                return false;
            }
        }
    } else {
        //authorized customer - get cart from database
        $itemID = SearchConfigurationInDataBase($variants, $productID);
        if ($itemID != -1) {
            // if this configuration exists in database
            $sql = '
                SELECT
                Quantity
                FROM ' . SHOPPING_CARTS_TABLE . '
                WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID;
            $q = db_query($sql);
            $row = db_fetch_row($q);
            $quantity = $row['Quantity'];
            if (CONF_CHECKSTOCK == 0 || $quantity + $count_to_order <= $is) {
                $sql = '
                    UPDATE ' . SHOPPING_CARTS_TABLE . '
                    SET Quantity=' . (int) ($row[0] + $count_to_order) . '
                    WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID;
                db_query($sql);
            } else {
                return false;
            }
        } else {
            //insert new item
            $count_to_order = $min_order_amount;
            if (CONF_CHECKSTOCK == 0 || $is >= $count_to_order) {
                $itemID = InsertNewItem($variants, $productID);
                InsertItemIntoCart($itemID);
                $sql = '
                    UPDATE ' . SHOPPING_CARTS_TABLE . '
                    SET Quantity=' . (int) $count_to_order . '
                    WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID;
                db_query($sql);
            } else {
                return false;
            }
        }
    }
    return true;
}