Cara Membuat Shopping Cart Menggunakan PHP

Di kesempatan kali ini kita akan belajar mengenai Cara Membangun Shopping Cart Menggunakan PHP. Shopping Cart adalah bagian yang tak terpisahkan di sebuah blog jual beli atau di sebuah blog e-commerce. Pada shopping cart atau lazim disebut keranjang belanja. Keranjang belanja berfungsi menampung item-item alternatif barang yang akan dibeli oleh user atau pengguna blog e-commerce.



Cara Membangun Shopping Cart Menggunakan PHP ialah selaku berikut langkah-langkahnya:



Pertama-tama-tama coba rekan rekan merancang database kemudian merancang tabel-tabel seperti tampilan dibawah ini:



Pada



Berikut ini ialah detail field yang terdapat di tabel-tabel diatasPada



PadaPadaPada



Kalau telah berikutnya merancang koneksi ke database seperti code dibawah ini:



Config.php



<?php 
// merancang konfigurasi koneksi database
$dbHost = localhost;
$dbUsername = root;
$dbPassword = ;
$dbName = belajar-php;

// merancang koneksi ke database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

// mengerjakan pengecekan koneksi
if ($db->connect_error) {
die(Connection failed: . $db->connect_error);
}


Kalau telah berikutnya rekan rekan dapat menambahkan beberapa data ke pada tabel product agar dapat ditampilkan.



langkah berikutnya bikin file index.php seperti code berikut ini:



<?php 
// Initialisasi shopping cart class
include_once 'Cart.php';
$cart = new Cart;

// Include koneksi database di file config
require_once 'Config.php';
?>

<!DOCTYPE html>
<html lang=en>
<head>
<title>Membangun Shopping Cart Menggunakan PHP</title>
<meta charset=utf-8>

<!-- Bootstrap core CSS -->
<link href=https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css rel=stylesheet>

<!-- Modifikasi style -->
<link href=style.css rel=stylesheet>

<!-- Fontawesome Icon -->
<link rel=stylesheet href=https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css>

</head>
</head>
<body>
<div class=container>
<h1>PRODUCTS</h1>

<!-- keranjang belanja -->
<div class=cart-view>
<a href=View_cart.php title=Cara Membangun Shopping Cart Menggunakan PHP><i class=fas fa-shopping-cart></i> (<?php echo ($cart->total_items() > 0)?$cart->total_items().' Items':'Empty'; ?>)</a>
</div>

<!-- Product list -->
<div class=row col-lg-12>
<?php
// ambil data products from database
$result = $db->query(SELECT * FROM art_products ORDER BY id DESC LIMIT 10);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<div class=card col-lg-4 mr-2>
<div class=card-body>
<h5 class=card-title><?php echo $row[name]; ?></h5>
<h6 class=card-subtitle mb-2 text-muted><?php echo 'Rp. '.number_format($row[price],0, '.','.'); ?></h6>
<small class=card-text><?php echo $row[description]; ?></small>
<a href=cartAction.php?action=addToCartid=<?php echo $row[id]; ?> class=btn btn-primary mt-2>Add to Cart</a>
</div>
</div>
<?php } }else{ ?>
<p>Product sedang tak terdapat.....</p>
<?php } ?>
</div>
</div>
</body>
</html>


Di file index.php, dapat dilihat ada command query bagi menampilkan data yang berasal dari tabel product. Jadi sekiranya ditampilan di browser karenanya akan seperti berikut ini:



PadaBaiklah langkah berikutnya kita mesti merancang sebuah Class Cart, bagi library cart yang akan berisi beberapa function.



<?php 
// Membangun session
if(!session_id()){
session_start();
}

class Cart {
protected $cart_contents = array();

public function __construct(){
// memperoleh nilai di cart berbentuk array menangkap dari sebuah session
$this->cart_contents = !empty($_SESSION['cart_contents'])?$_SESSION['cart_contents']:NULL;
if ($this->cart_contents === NULL){
// mengatur value awal = 0
$this->cart_contents = array('cart_total' => 0, 'total_items' => 0);
}
}

/* isi di keranjang : mengembalikan array di cart */
public function contents(){
// utk mengatur ulang nilai di isi cart
$cart = array_reverse($this->cart_contents);

// menghapus nilai supaya tak terjadi kesalahan dikala menampilkan kembali
unset($cart['total_items']);
unset($cart['cart_total']);

return $cart;
}


public function get_item($row_id){
return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->cart_contents[$row_id]))
? FALSE
: $this->cart_contents[$row_id];
}


public function total_items(){
return $this->cart_contents['total_items'];
}


public function total(){
return $this->cart_contents['cart_total'];
}


public function insert($item = array()){
if(!is_array($item) OR count($item) === 0){
return FALSE;
}else{
if(!isset($item['id'], $item['name'], $item['price'], $item['qty'])){
return FALSE;
}else{

$item['qty'] = (float) $item['qty'];
if($item['qty'] == 0){
return FALSE;
}

$item['price'] = (float) $item['price'];

$rowid = md5($item['id']);

$old_qty = isset($this->cart_contents[$rowid]['qty']) ? (int) $this->cart_contents[$rowid]['qty'] : 0;

$item['rowid'] = $rowid;
$item['qty'] += $old_qty;
$this->cart_contents[$rowid] = $item;


if($this->save_cart()){
return isset($rowid) ? $rowid : TRUE;
}else{
return FALSE;
}
}
}
}


public function update($item = array()){
if (!is_array($item) OR count($item) === 0){
return FALSE;
}else{
if (!isset($item['rowid'], $this->cart_contents[$item['rowid']])){
return FALSE;
}else{
// prep the quantity
if(isset($item['qty'])){
$item['qty'] = (float) $item['qty'];
// remove the item from the cart, if quantity is zero
if ($item['qty'] == 0){
unset($this->cart_contents[$item['rowid']]);
return TRUE;
}
}


$keys = array_intersect(array_keys($this->cart_contents[$item['rowid']]), array_keys($item));

if(isset($item['price'])){
$item['price'] = (float) $item['price'];
}

foreach(array_diff($keys, array('id', 'name')) as $key){
$this->cart_contents[$item['rowid']][$key] = $item[$key];
}

$this->save_cart();
return TRUE;
}
}
}


protected function save_cart(){
$this->cart_contents['total_items'] = $this->cart_contents['cart_total'] = 0;
foreach ($this->cart_contents as $key => $val){

if(!is_array($val) OR !isset($val['price'], $val['qty'])){
continue;
}

$this->cart_contents['cart_total'] += ($val['price'] * $val['qty']);
$this->cart_contents['total_items'] += $val['qty'];
$this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);
}


if(count($this->cart_contents) <= 2){
unset($_SESSION['cart_contents']);
return FALSE;
}else{
$_SESSION['cart_contents'] = $this->cart_contents;
return TRUE;
}
}


public function remove($row_id){

unset($this->cart_contents[$row_id]);
$this->save_cart();
return TRUE;
}


public function destroy(){
$this->cart_contents = array('cart_total' => 0, 'total_items' => 0);
unset($_SESSION['cart_contents']);
}
}


Saya kira lumayan pada pembahasan mengenai Cara Membangun Shopping Cart Menggunakan PHP. Dan akan kita lanjutkan di artikel berikutnya.



terima kasih




Sumber https://kursuswebsite.org

Popular posts from this blog

Perbedaan Antara Keyup dan Keydown Pada jQuery

Membuat Table Warna Berselang dengan PHP