我正在处理一个将订单项详细信息存储在cookie中的现有代码。
在控制器中:
public function actionAdd2cart($id, $url, $instructor_id, $location_id, $start_time, $date1) {
$ep = Yii::$app->getTable->settings('general','enable_payment');
$order_item = \Yii::$app->getRequest()->getCookies()->getValue('order_item');
{
$model = ClassDuration::find()->where(['id' => $id])->One();
$model1 = Instructor::find()->where(['user_id' => $instructor_id])->One();
$time = strtotime($start_time);
$duration = $model1['instructor_duration'];
$endTime = date("h:i A", strtotime('+' . $duration . ' minutes', $time));
$fees = Instructor::find()->where(['user_id' => $model['instructor_id']])->one();
$order_item[] = [
'duration' => date('h:i A', strtotime($start_time)) . '-' . $endTime,
'starttime' => $start_time,
'endtime' => $endTime,
'id' => $model->id,
//'price' => $fees['instructor_fees'], this is disabled as payment info is not needed and to increase the cookie size
'q' => 1,
'date' => $model->start_date,
'instructor_id' => $instructor_id,
'location_id' => $location_id,
'date1' => $date1,
'datetime' => $date1 . "-" . $start_time . "i" . $instructor_id,
]; //product details array
}
/********** cookies for product details **********/
$cookie = new \yii\web\Cookie([
'name' => 'order_item',
'value' => $order_item,
'expire' => time() + (60 * 60 * 24),
]);
Yii::$app->getResponse()->getCookies()->add($cookie); // add product deatils to cookies
if ($url == 'view') {
return $this->redirect(Yii::$app->request->referrer);
} else {
// echo 1;
}
}
现在,此代码面临的问题就像在将商品添加到购物车时一样,它允许根据不同的浏览器在购物车中添加6-8件商品,并且由于Cookie大小的限制,我无法添加除此之外,还有更多商品可以购物。
我正在寻找一种方法,比如可以在设置cookie之前压缩cookie值,而在访问cookie时将其解压缩。
或其他合适的解决方案。