How to verify if shopping cart already exist

Hi Friends!

I’m working on an ecommerce project in yii. I put an adding to shopping cart in my project. And now,

I want to know how to make a condition if cart exists or not and change the link "add to cart" to "product is already in your cart".

here is my controller:




	public function actionCreateCart()

	{

		$model=$this->loadModel($_POST['code_produit']);

		if(!is_numeric($_POST['amount']) || $_POST['quantiteShop'] == 0) {

		Shop::setFlash('Select an amount');

			$this->redirect(array( 

			'view', 'id' => $_POST['code_produit']));

}

		$this->redirect(array( 

		'view', 'id' => $_POST['code_produit']));

		}

		$cart = Shop::getCartContent();


		// remove potential clutter

		if(isset($_POST['yt0']))

			unset($_POST['yt0']);

		if(isset($_POST['yt1']))

			unset($_POST['yt1']);


		$cart[] = $_POST;

                

		Shop::setCartcontent($cart);

		Shop::setFlash(Shop::t('The product has been added to the shopping cart'));

		$this->redirect(array('index'));

	}



View:




<?php

	$this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

	((UserModule::isAdmin())

		?'code_produit'

		:array(

          'name'=>'code_produit',

          'visible'=>false

            )),

		'libelle_produit',

		(($model->description != null)

		?'description'

		:array(

        'name'=>'code_cat',

		'visible'=> false

		)),

		array(

        'name'=>'code_cat', 

        'value'=>isset($model->code_cat)?CHtml::encode($model->codeCat->libelle_cat):"",

        ),

		((UserModule::isAdmin())

		?'quantite'

		:array(

          'name'=>'amount',

          'visible'=>false

            )),

	    array(

        'name'=>'price', 

        'value'=>isset($model->price)?CHtml::encode($model->price),

        ),		

))); ?>


<div class="shopQuantité"><?php echo CHtml::label('Select an amount', 'ShoppingCart_amount');

echo ': ';

echo CHtml::hiddenField('code_produit', $model->code_produit);

echo CHtml::dropDownList('amount',1, array('' , 1 , 2 , 3, 4, 5, 6, 7, 8, 9 ));

echo Shop::renderFlash();?>

</div>


		<div class="addToCart">

		<?php echo CHtml::submitButton('add to cart');?>

		</div>

<?php echo CHtml::endForm(); ?>



Model:




<?php

class Shopping {

    public static function getCartContent() {

        if (is_string(Yii::app()->user->getState('cart')))

            return json_decode(Yii::app()->user->getState('cart'), true);

        else

            return Yii::app()->user->getState('cart');

    }


    public static function setCartContent($cart) {

        return Yii::app()->user->setState('cart', json_encode($cart));

    }

    public static function priceFormat($price) {

        $montant = sprintf('%.2f', $price);

        if (Yii::app()->language == 'de')

            $price= str_replace('.', ',', $price);


        $price.= ' ' . Shop::module()->currencySymbol;


        return $price;

    }


    public static function getPriceTotal() {

        $priceTotal= 0;

        foreach (Shopping::getCartContent() as $produit) {

            $model = Produit::model()->findByPk($produit['code_produit']);

            $priceTotal+= $model->getPrice(@$produit['amount']);

        }


        $priceTotal= Shop::t('Price total: {total}', array(

                    '{total}' => Shopping::priceFormat($priceTotal),

        ));

        $priceTotal.= '<br />';


        return $priceTotal;

    }

}



could someone help me with this please?

thanks!

hi

use this code in your model :




    public function rules() {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            

            array('cart', 'unique', 'attributeName' => 'cart',

                'message' => 'this code is invalid'),


        );

    }



You are using yii-shop module, its very poorly organized/written, you can add a custom method in models/shop.php


public static function check($id)

{

   // if the item is cart return true

   // otherwise return false

}