<?php
/**
 * Created by PhpStorm.
 * User: vitaliy
 * Date: 31.03.16
 * Time: 12:20
 */

namespace common\widgets;
use common\modules\product\models\ProductVariant;
use yii\base\Widget;


class BasketHead extends Widget
{

    public function init(){

        parent::init();

    }


    public function run()
    {
        $sessionData = \Yii::$app->session->get('order');
        unset($sessionData['order_id']);
        $count = count($sessionData);
        $price = 0;
        if(is_array($sessionData) && !empty($sessionData)){

            $variant = ProductVariant::find()->where(['product_variant_id'=>array_keys($sessionData)])->indexBy('product_variant_id')->all();


            foreach ($sessionData as $k => $item) {
                $sessionData[$k]['item'] = $variant[$k];
                $price += $variant[$k]->price * $sessionData[$k]['num'];
            }


            return $this->render('basket_head',[
                'items'=>$sessionData,
                'count' => $count,
                'price' => $price
            ]);

        }

    }
}