Add a page field value to session from plugin file

Hello,

I’ve created a plugin folder with a php file inside. /plugins/cart/cart.php

Basically, I’m sending AJAX posts via javascript, triggering functions I’ve put in there.

What I want to achieve :

  • I click a button on my product page
  • It triggers an AJAX post, with some variables (the ID of the product, the selected size)
  • from here I want my php function to look for the price of the given product, then add a bunch of stuff to a session.

My function looks like this :

s::start();

(...)

function ajouterArticle($id_entree, $id_produit, $taille_produit) {

    // Find the price
    $site  = site();
    $pages = $site->pages();
    $prix_produit = $pages->find('eshop')->find($id_produit)->prix();

    if (creationPanier()) {
        $indexProduit = array_search($id_entree, $_SESSION['panier']['id_entree']);
        if ($indexProduit !== false) {
            $_SESSION['panier']['qte_produit'][$indexProduit] += 1;
        }
        else {
            array_push($_SESSION['panier']['id_entree'], $id_entree);
            array_push($_SESSION['panier']['nom_produit'], $id_produit);
            array_push($_SESSION['panier']['taille_produit'], $taille_produit);
            array_push($_SESSION['panier']['prix_produit'], $prix_produit);
            array_push($_SESSION['panier']['qte_produit'], 1);
        }
        $response = "Product added";
    }
    else {
        $response = "Nope, can't do that";
    }
    return $response;
}

If I echo $prix_produit, it gives me 50.
If I return $prix_produit as a response, my console.log gives me 50.

Though, the following line :
array_push($_SESSION['panier']['prix_produit'], $prix_produit);

Won’t add those 50 to the session and prevent the function from running completely.

What am I doing wrong here ?

Thanks !

Oh, my bad, dumb question,

(string)$prix_produit

works fine.

If $prix_produit is a field you can also do this:

$prix_produit->value();
1 Like

Thanks for this cleaner way :slight_smile: