Cart.php 6.0 KB
Newer Older
R
init  
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\services;
use Yii;
use yii\base\InvalidValueException;
use yii\base\InvalidConfigException;
use fec\helpers\CSession;
14 15
use fecshop\models\mysqldb\Cart as MyCart;
use fecshop\models\mysqldb\Cart\Item as MyCartItem;
R
init  
root 已提交
16 17 18 19 20 21 22 23 24
/**
 * Cart services
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Cart extends Service
{
	
	/**
25
	 * 将某个产品加入到购物车中
R
init  
root 已提交
26
	 * @property $item|Array
27 28 29 30 31
	 * $item = [
	 *		'product_id' 		=> 22222,
	 *		'custom_option_sku' => ['color'=>'red','size'=>'l'],
	 *		'qty' 				=> 22,
	 * ];
R
init  
root 已提交
32
	 */
R
update  
root 已提交
33
	protected function actionAddProductToCart($item){
34 35 36 37
		$product = Yii::$service->product->getByPrimaryKey($item['product_id']);
		$productValidate = Yii::$service->cart->info->validateProduct($item,$product);
		if(!$productValidate){
			return false;
R
root 已提交
38
		}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		//echo $item['custom_option_sku'];
		if(isset($item['custom_option_sku']) && !empty($item['custom_option_sku'])){
			$custom_option_sku = Yii::$service->cart->info->getCustomOptionSku($item,$product);
			if(!$custom_option_sku){
				return false;
			}
			$item['custom_option_sku'] = $custom_option_sku;
		}
		
		
		Yii::$service->cart->quote->getMyCart();
		$innerTransaction = Yii::$app->db->beginTransaction();
		try {
			Yii::$service->cart->quoteItem->addItem($item);
			$innerTransaction->commit();
		} catch (Exception $e) {
			$innerTransaction->rollBack();
R
root 已提交
56
		}
57 58 59
		return true;
		
	}
R
root 已提交
60
	
61 62 63 64 65 66
	# 得到购物车中产品的个数
	protected function actionGetCartItemQty(){
		return Yii::$service->cart->quote->getCartItemCount();
		
	}
	
R
root 已提交
67 68 69 70
	protected function actionGetCartInfo(){
		return Yii::$service->cart->quote->getCartInfo();
	}
	
71 72 73 74 75 76 77 78
	/**
	 * 初始化
	 */
	/*
	protected function initAddItem($product_id,$co_sku,$qty){
		$product = $this->_product;
		
		$base_price = Yii::$service->product->price->getFinalPrice(
R
root 已提交
79 80
			$product['price'],$product['special_price']	,
			$product['special_from'],$product['special_to']	,
81
			$qty, $product['tier_price']
R
root 已提交
82
		);
83
		$base_row_total = $base_price * $qty;
R
init  
root 已提交
84
		
85 86
		$current_currency_price = Yii::$service->page->currency->getCurrentCurrencyPrice($base_price);
		$row_total = $current_currency_price * $qty;
R
init  
root 已提交
87
		
R
root 已提交
88
		$weight = $product['weight'];
89
		$row_weight = $weight * $qty;
R
root 已提交
90
		
91 92 93 94 95
		$this->_add_item = [
			'product_id'		=> $product_id,
			'co_sku'			=> $co_sku,
			'qty' 				=> $qty,
		];
R
init  
root 已提交
96
	}
97 98
	*/
	
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	/**
	 * @property $item_id | Int 购物车产品表的id字段
	 * 通过item id 将购物车中的某个产品的个数加一
	 */
	protected function actionAddOneItem($item_id){
		$innerTransaction = Yii::$app->db->beginTransaction();
		try {
			$status = Yii::$service->cart->quoteItem->addOneItem($item_id);
			if(!$status){
				$innerTransaction->rollBack();
				return false;
			}
			Yii::$service->cart->quote->computeCartInfo();
			$innerTransaction->commit();
			return true;
		} catch (Exception $e) {
			$innerTransaction->rollBack();
		}
		return false;
		
	}
	/**
	 * @property $item_id | Int 购物车产品表的id字段
	 * 通过item id 将购物车中的某个产品的个数减一
	 */
	protected function actionLessOneItem($item_id){
		$innerTransaction = Yii::$app->db->beginTransaction();
		try {
			$status = Yii::$service->cart->quoteItem->lessOneItem($item_id);
			if(!$status){
				$innerTransaction->rollBack();
				return false;
			}
			Yii::$service->cart->quote->computeCartInfo();
133
	
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
			$innerTransaction->commit();
			return true;
		} catch (Exception $e) {
			$innerTransaction->rollBack();
		}
		return false;
	}
	
	/**
	 * @property $item_id | Int 购物车产品表的id字段
	 * 通过item id 删除购物车中的某个产品
	 */
	protected function actionRemoveItem($item_id){
		$innerTransaction = Yii::$app->db->beginTransaction();
		try {
			$status = Yii::$service->cart->quoteItem->removeItem($item_id);
			if(!$status){
				$innerTransaction->rollBack();
				return false;
			}
			Yii::$service->cart->quote->computeCartInfo();
			$innerTransaction->commit();
			return true;
		} catch (Exception $e) {
			$innerTransaction->rollBack();
		}
		return false;
	}
R
init  
root 已提交
162
	
R
update  
root 已提交
163
	protected function actionGetUserCartInfo(){
R
init  
root 已提交
164 165 166 167
		
		
	}
	
R
update  
root 已提交
168
	protected function actionChangeItemQty($sku){
R
init  
root 已提交
169 170 171 172 173 174
		
	}
	
	/**
	 *  merge cart , if current cart currency is not equals to user cart currency when user login account.
	 */
R
update  
root 已提交
175
	protected function actionMergeCartAfterUserLogin(){
R
init  
root 已提交
176 177 178 179 180 181 182 183
		
		
	}
	
	/**
	 * change current cart currency 
	 * 1. check if currency is allowed to change.
	 */
R
update  
root 已提交
184
	protected function actionChangeCartCurrency(){
R
init  
root 已提交
185 186 187 188 189 190 191 192
		
		
	}
	
	/**
	 * @property $language|String
	 * change current language , cart product  language change to current language.
	 */
R
update  
root 已提交
193
	protected function actionChangeProductLanguage($language=''){
R
init  
root 已提交
194 195 196 197 198 199 200 201
		
		
	}
	
	/**
	 * @property $address|Array
	 * save cart address.like,,  customer name,tel,email,address ,,etc,,.
	 */
R
update  
root 已提交
202
	protected function actionSaveCartAddress($address){
R
init  
root 已提交
203 204 205 206 207 208 209 210 211 212 213
		
		
	}
	
	/**
	 * @property $shippingId | Int 
	 * 1.check if $shippingId is effective
	 * 2.add or change shipping to cart.
	 * 3.change shipping cost after change
	 * 
	 */
R
update  
root 已提交
214
	protected function actionSaveCartShipping($shippingId){
R
init  
root 已提交
215 216 217 218 219 220 221 222 223
		
		
	}
	
	/**
	 * @property $payment | Int 
	 * 1.check if $paymentId is effective
	 * 2.add or change payment to cart.
	 */
R
update  
root 已提交
224
	protected function actionSaveCartPayment($paymentId){
R
init  
root 已提交
225 226 227 228
		
		
	}
	
229
	
R
init  
root 已提交
230 231 232 233
	
	/**
	 * clear cart product.
	 */
R
update  
root 已提交
234
	protected function actionClearCart(){
R
init  
root 已提交
235 236 237 238 239 240 241
		
		
	}
	
	/**
	 * generate order by current Cart.
	 */
R
update  
root 已提交
242
	protected function actionGenerateOrderByCart(){
R
init  
root 已提交
243 244 245 246 247 248 249 250 251 252
		
		
	}
	
	/**
	 * add cart items by pending order Id
	 * 1. check if the order is exist ,and belong to current customer.
	 * 2. get all item sku and custom option.
	 * 3. add to cart like in product page ,click add to cart button.
	 */
R
update  
root 已提交
253
	protected function actionAddItemsByPendingOrder($order_id){
R
init  
root 已提交
254 255 256 257 258 259
		
		
	}
	
	
}