1
votes

I'm using Yii2 for my e-commerce website.
Here I'm using omnilight/yii2-shopping-cart, but I am not sure why it does not save to session product's information.

I have used ajax request to put my product to shopping cart position.

function actionAddToCart() {

    $pid = $_REQUEST['pid'];
    $quantity = $_REQUEST['quantity'];

    $model = Product::findOne($pid);
    $model->quantity = $quantity;

    if ($model) {
        // @@@ Add Cookie Data here
        $cart = \Yii::$app->cart;

        $params = [];
        $params['price'] = $model->price;
        $params['quantity'] = $quantity;

        $cartPosition = $model->getCartPosition($params);

        $cart->put($cartPosition, $quantity);

        // var_dump($cart);
        // die();

        return $this->renderAjax('productView', [
            'product' => $model
        ]);
    }
}

When I get log of cart here, I can see that the product added to session.
But after loading productView, I am seeing that there is no product in the session.

Any help would be appreciated.

Here I'm using CartPositionInterface which have id, price, color, size, length, quantity as it's parameters. The cart returning below result :

object(yz\shoppingcart\ShoppingCart)#109 (6) {
  ["storeInSession"]=>
  bool(true)
  ["session"]=>
  object(yii\web\Session)#58 (6) {
    ["flashParam"]=>
    string(7) "__flash"
    ["handler"]=>
    NULL
    ["_cookieParams":"yii\web\Session":private]=>
    array(1) {
      ["httponly"]=>
      bool(true)
    }
    ["_hasSessionId":"yii\web\Session":private]=>
    bool(true)
    ["_events":"yii\base\Component":private]=>
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=>
    NULL
  }
  ["cartId"]=>
  string(23) "myshoppingCart"
  ["_positions":protected]=>
  array(1) {
    ["404c11b84c06bda0bf7464d5fdc85604"]=>
    object(common\models\ProductCartPosition)#111 (7) {
      ["_product":protected]=>
      NULL
      ["id"]=>
      int(1)
      ["price"]=>
      float(250)
      ["color"]=>
      string(1) "1"
      ["size"]=>
      string(1) "L"
      ["length"]=>
      string(2) "56"
      ["quantity"]=>
      string(1) "1"
    }
  }
  ["_events":"yii\base\Component":private]=>
  array(0) {
  }
  ["_behaviors":"yii\base\Component":private]=>
  array(0) {
  }
}

But when I get log from productView, it returns below result :

object(yz\shoppingcart\ShoppingCart)#49 (6) {
  ["storeInSession"]=>
  bool(true)
  ["session"]=>
  object(yii\web\Session)#52 (6) {
    ["flashParam"]=>
    string(7) "__flash"
    ["handler"]=>
    NULL
    ["_cookieParams":"yii\web\Session":private]=>
    array(1) {
      ["httponly"]=>
      bool(true)
    }
    ["_hasSessionId":"yii\web\Session":private]=>
    NULL
    ["_events":"yii\base\Component":private]=>
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=>
    NULL
  }
  ["cartId"]=>
  string(23) "aljazeera_shopping_cart"
  ["_positions":protected]=>
  array(0) {
  }
  ["_events":"yii\base\Component":private]=>
  array(0) {
  }
  ["_behaviors":"yii\base\Component":private]=>
  NULL
}

Please let me know if you have any advice.

2

2 Answers

0
votes

for sessione you should use

$session = new Session;
$session->open();
$value1 = $session['name1'];  // get session variable 'name1'
$value2 = $session['name2'];  // get session variable 'name2'
foreach ($session as $name => $value) // traverse all session variables
$session['name3'] = $value3;  // set session variable 'name3'

see this for ref. http://www.yiiframework.com/doc-2.0/yii-web-session.html

0
votes

This happens on my localhost since in my xampp settings, php session was closed. I'm not sure, but after trying to figure it out, it started saving session.

I think it started workign after I have changed session_auto_start flag to 1 in php.ini of xampp directory.