Oziris
July 28, 2022, 7:08pm
1
Hello
I’m trying to install the Cartkit Plugin for Kirby 3.7.2. This plugin is just a few lines into an index file (site/plugins/cart/index.php). The rest of the plugin is just two controllers between the product page and the cart page.
When I send the form to get the product ID , I don’t know why, I can’t get the $product
, but i get the $quantity
and $id
on the cart page (nothing returned).
I would just ask to somebody if you can take a look at this file, if something is weird or not compatible with php7.4 or Kirby 3.
Thx a lot !
<?php
// Demarre la session
function get_cart() {
s::start();
$cart = s::get('panier', array());
return $cart;
}
// Defini les actions ajouter/augmenter, diminuer, mettre a jour & supprimer un article
function cart_logic() {
$cart = s::get('panier', array());
if(isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
$id = $_REQUEST['id'];
switch ($action) {
// Ajoute un article / Augmente la quantite d'un article
case 'add':
if(isset($cart[$id])) {
$cart[$id]++;
} else {
$cart[$id] = 1;
}
break;
// Diminue la quantite d'un article
case 'remove':
if(isset($cart[$id])) {
$cart[$id]--;
} else {
$cart[$id] = 1;
}
break;
// Met a jour la quantite d'un article
case 'update':
if(isset($_REQUEST['quantity'])) {
$quantity = intval($_REQUEST['quantity']);
if ($quantity < 1) {
unset($cart[$id]);
} else {
$cart[$id] = $quantity;
}
}
break;
// Supprime un article
case 'delete':
if (isset($cart[$id])) {
unset($cart[$id]);
}
break;
}
s::set('panier', $cart);
}
return $cart;
}
// Calcule le nombre total d'articles present dans le panier
function cart_count() {
$cart = s::get('panier', array());
$count = 0;
foreach ($cart as $id => $quantity) {
$count += $quantity;
}
return $count;
}
// Calcule le prix total des articles sans les frais de livraison
function cart_calc_total() {
$site = site();
$pages = $site->pages();
$products = $site->find('objects')->children()->listed();
$cart = s::get('panier', array());
$count = 0; $total = 0;
foreach($cart as $id => $quantity){
if($product = $products->find($id)){
$count += $quantity;
$prodtotal = floatval($product->price()->value) * $quantity;
$total += $prodtotal;
}
}
return $total;
}
// Calcul de la valeur HT d'un prix TTC donné
function cart_ht($ttc, $vat) {
$ht = $ttc / (1 + $vat/100);
return $ht;
}
// Calcul de la valeur de la taxe incluse dans le prix TTC donné
function cart_vat_incl($ttc, $vat) {
$ht = $ttc / (1 + $vat/100);
$tax_incl = $ttc - $ht;
return $tax_incl;
}
// Calcul de la valeur de la TVA à ajouter à un prix net
function cart_vat($net, $vat) {
$tax = $net * $vat/100;
return $tax;
}
// Frais de livraison
function cart_postage($total) {
$postage;
switch ($total) {
case ($total < 10):
$postage = 2.5;
break;
case ($total < 30):
$postage = 5.5;
break;
case ($total < 75):
$postage = 8;
break;
case ($total < 150):
$postage = 11.56;
break;
case ($total < 300):
$postage = 28.30;
break;
default:
$postage = 40.75;
}
return $postage;
}
This is old Kirby 2 code, not compatible with Kirby 3.
This for example is outdated, Kirby 3 $session | Kirby CMS
Oziris
July 28, 2022, 7:37pm
3
Thanks !
I’ve replaced
s::start() by $session = $kirby->session();
s::get() by $session->get
s::set() by $session->set
But the debugger says $session
is undefined on
function cart_logic() {
$cart = $session->get('panier', array());
In your function, $session is not definded
Oziris
July 28, 2022, 7:54pm
5
Yes and I don’t know how to defined it because I’ve already set
function get_cart() {
$session = $kirby->session();
$cart = $session->get('panier', array());
return $cart;
}
a few lines above (I have a low level in php)
Well, but you cannot use the variables you defined outside a function inside the function, because there they are not in the scope. Therefore, you cannot use $kirby either. Read about variable scope in PHP.
Oziris
July 28, 2022, 9:47pm
7
Ok then I’ve set $kirby
and $session
variables inside each function (thinking it’s the right way). The code looks like this now but I cannot get the $product
variable either.
<?php
function get_cart() {
$kirby = kirby();
$session = $kirby->session();
$cart = $session->get('cart', array());
return $cart;
}
// Defini les actions ajouter/augmenter, diminuer, mettre a jour & supprimer un article
function cart_logic() {
$kirby = kirby();
$session = $kirby->session();
$cart = $session->get('cart', array());
if(isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
$id = $_REQUEST['id'];
switch ($action) {
// Ajoute un article / Augmente la quantite d'un article
case 'add':
if(isset($cart[$id])) {
$cart[$id]++;
} else {
$cart[$id] = 1;
}
break;
// Diminue la quantite d'un article
case 'remove':
if(isset($cart[$id])) {
$cart[$id]--;
} else {
$cart[$id] = 1;
}
break;
// Met a jour la quantite d'un article
case 'update':
if(isset($_REQUEST['quantity'])) {
$quantity = intval($_REQUEST['quantity']);
if ($quantity < 1) {
unset($cart[$id]);
} else {
$cart[$id] = $quantity;
}
}
break;
// Supprime un article
case 'delete':
if (isset($cart[$id])) {
unset($cart[$id]);
}
break;
}
$session->set('cart', $cart);
}
return $cart;
}
// Calcule le nombre total d'articles present dans le panier
function cart_count() {
$kirby = kirby();
$session = $kirby->session();
$cart = $session->get('cart', array());
$count = 0;
foreach ($cart as $id => $quantity) {
$count += $quantity;
}
return $count;
}
// Calcule le prix total des articles sans les frais de livraison
function cart_calc_total() {
$kirby = kirby();
$session = $kirby->session();
$site = site();
$pages = $site->pages();
$products = $site->find('objects')->children()->listed();
$cart = $session->get('cart', array());
$count = 0; $total = 0;
foreach($cart as $id => $quantity){
if($product = $products->find($id)){
$count += $quantity;
$prodtotal = floatval($product->price()->value) * $quantity;
$total += $prodtotal;
}
}
return $total;
}
// Calcul de la valeur HT d'un prix TTC donné
function cart_ht($ttc, $vat) {
$ht = $ttc / (1 + $vat/100);
return $ht;
}
// Calcul de la valeur de la taxe incluse dans le prix TTC donné
function cart_vat_incl($ttc, $vat) {
$ht = $ttc / (1 + $vat/100);
$tax_incl = $ttc - $ht;
return $tax_incl;
}
// Calcul de la valeur de la TVA à ajouter à un prix net
function cart_vat($net, $vat) {
$tax = $net * $vat/100;
return $tax;
}
// Frais de livraison
function cart_postage($total) {
$postage;
switch ($total) {
case ($total < 10):
$postage = 2.5;
break;
case ($total < 30):
$postage = 5.5;
break;
case ($total < 75):
$postage = 8;
break;
case ($total < 150):
$postage = 11.56;
break;
case ($total < 300):
$postage = 28.30;
break;
default:
$postage = 40.75;
}
return $postage;
}