Dear all,
I've been working on an e-commerce project for months. So here I am trying to find help in setting minimum order quantity for this project based on codeigniter 3 cart library.
The controller and view are like so:
Code:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th><?= lang('product') ?></th>
<th><?= lang('title') ?></th>
<th><?= lang('quantity') ?></th>
<th><?= lang('price') ?></th>
<th><?= lang('total') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($cartItems['array'] as $item) { ?>
<tr>
<td class="relative">
<input type="hidden" name="id[]" value="<?= $item['id'] ?>">
<input type="hidden" name="quantity[]" value="<?= $item['num_added'] ?>">
<img class="product-image" src="<?= base_url('/path/to_images/' . $item['image']) ?>" alt="">
<a href="<?= base_url('home/removeFromCart?delete-product=' . $item['id'] . '&back-to=shopping-cart') ?>" class="btn btn-xs btn-danger remove-product">
<span class="glyphicon glyphicon-remove"></span>
</a>
</td>
<td><a href="<?= LANG_URL . '/' . $item['url'] ?>"><?= $item['title'] ?></a></td>
<td>
<a class="btn btn-xs btn-primary refresh-me add-to-cart <?= $item['quantity'] <= $item['num_added'] ? 'disabled' : '' ?>" data-id="<?= $item['id'] ?>" href="javascript:void(0);">
<span class="glyphicon glyphicon-plus"></span>
</a>
<span class="quantity-num">
<?= $item['num_added'] ?>
</span>
<a class="btn btn-xs btn-danger" onclick="removeProduct(<?= $item['id'] ?>, true)" href="javascript:void(0);">
<span class="glyphicon glyphicon-minus"></span>
</a>
</td>
<td><?= $item['price'] . CURRENCY ?></td>
<td><?= $item['sum_price'] . CURRENCY ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="4" class="text-right"><?= lang('total') ?></td>
<td><?= $cartItems['finalSum'] . CURRENCY ?></td>
</tr>
</tbody>
</table>
</div>
CONTROLLER:
PHP Code:
/*
* Here is shopping cart class that manages products
*/
class ShoppingCart
{
protected $CI;
public $sumValues;
/*
* 1 month expire time
*/
private $cookieExpTime = 1000000;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->model('admin/admin_model');
}
public function manageShoppingCart()
{
if ($_POST['action'] == 'add') {
if (!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = array();
}
@$_SESSION['shopping_cart'][] = (int) $_POST['article_id'];
}
if ($_POST['action'] == 'remove') {
if (($key = array_search($_POST['article_id'], $_SESSION['shopping_cart'])) !== false) {
unset($_SESSION['shopping_cart'][$key]);
}
}
@set_cookie('shopping_cart', serialize($_SESSION['shopping_cart']), $this->cookieExpTime);
$result = 0;
if (!empty($_SESSION['shopping_cart'])) {
$result = $this->getCartItems();
}
// get items from db and add him to cart products list from ajax
$loop = $this->CI->loop;
$loop::getCartItems($result);
}
public function removeFromCart()
{
$count = count(array_keys($_SESSION['shopping_cart'], $_GET['delete-product']));
$i = 1;
do {
if (($key = array_search($_GET['delete-product'], $_SESSION['shopping_cart'])) !== false) {
unset($_SESSION['shopping_cart'][$key]);
}
$i++;
} while ($i <= $count);
@set_cookie('shopping_cart', serialize($_SESSION['shopping_cart']), $this->cookieExpTime);
}
public function getCartItems()
{
if ((!isset($_SESSION['shopping_cart']) || empty($_SESSION['shopping_cart'])) && get_cookie('shopping_cart') != NULL) {
$_SESSION['shopping_cart'] = unserialize(get_cookie('shopping_cart'));
} elseif (!isset($_SESSION['shopping_cart']) || !is_array($_SESSION['shopping_cart'])) {
return 0;
}
$result['array'] = $this->CI->Cart_model->getShopItems(array_unique($_SESSION['shopping_cart']));
if (empty($result['array'])) {
unset($_SESSION['shopping_cart']);
@delete_cookie('shopping_cart');
return 0;
}
$count_articles = array_count_values($_SESSION['shopping_cart']);
$this->sumValues = array_sum($count_articles);
$finalSum = 0;
foreach ($result['array'] as &$article) {
$article['num_added'] = $count_articles[$article['id']];
$article['price'] = $article['price'] == '' ? 0 : $article['price'];
$article['sum_price'] = $article['price'] * $count_articles[$article['id']];
$finalSum = $finalSum + $article['sum_price'];
$article['sum_price'] = number_format($article['sum_price'], 2);
$article['price'] = $article['price'] != '' ? number_format($article['price'], 2) : 0;
}
$result['finalSum'] = number_format($finalSum, 2);
return $result;
}
public function clearShoppingCart()
{
unset($_SESSION['shopping_cart']);
@delete_cookie('shopping_cart');
if ($this->CI->input->is_ajax_request()) {
echo 1;
}
}
}
What I'm trying to achieve is setting minimum and maximum order limits across website by manipulating "num_added". So that a predefined quantity is added at the begining with an increment of 1 thereafter. Then the maximum is also capped to avoid overselling.
I hope someone can give me a hand here. Oh and I know there are other cart solutions out there, but I want this, provided I can get a little help to enhance its features.
Thanks for any help and stay safe in this difficult times.