提交 42f06d62 编写于 作者: R root

fecshop的购物车页面的代码添加

上级 1c71bafe
......@@ -20,6 +20,7 @@ class CustomOption {
public $custom_option;
public $attr_group;
public $product_id;
public $middle_img_width; # 图片的宽度。
protected $_custom_option_arr;
......@@ -28,6 +29,7 @@ class CustomOption {
//var_dump($items);exit;
return [
'items' => $items ,
'product_id' => $this->product_id,
'custom_option_arr' => json_encode($this->_custom_option_arr),
'middle_img_width' => $this->middle_img_width,
];
......
......@@ -29,6 +29,36 @@ class ProductController extends AppfrontController
return $this->render($this->action->id,$data);
}
# ajax 得到产品加入购物车的价格。
public function actionGetcoprice(){
$custom_option_sku = Yii::$app->request->get('custom_option_sku');
$product_id = Yii::$app->request->get('product_id');
$qty = Yii::$app->request->get('qty');
$cart_price = 0;
$custom_option_price = 0;
$product = Yii::$service->product->getByPrimaryKey($product_id);
$cart_price = Yii::$service->product->price->getCartPriceByProductId($product_id,$qty,$custom_option_sku);
if(!$cart_price){
return;
}
$price_info = [
'price' => $cart_price,
];
$priceView = [
'view' => 'catalog/product/index/price.php'
];
$priceParam = [
'price_info' => $price_info,
];
echo json_encode([
'price' =>Yii::$service->page->widget->render($priceView,$priceParam),
]);
exit;
}
}
......
<?php
/*
* 存放 一些基本的非数据库数据 如 html
* 都是数组
*/
namespace fecshop\app\appfront\modules\checkout\block\cart;
use Yii;
use fec\helpers\CModule;
use fec\helpers\CRequest;
class Index {
public function getLastData(){
$this->initHead();
return [
];
}
public function getCartInfo(){
# 得到单个产品的价格
//Yii::$service->product->price->getCartPriceByProductId($productId,$qty,$custom_option_sku);
}
public function initHead(){
Yii::$app->view->registerMetaTag([
'name' => 'keywords',
'content' => 'checkout cart',
]);
Yii::$app->view->registerMetaTag([
'name' => 'description',
'content' => 'checkout cart page',
]);
$this->_title = 'checkout cart page';
Yii::$app->view->title = $this->_title;
}
}
......@@ -19,6 +19,13 @@ class CartController extends AppfrontController
{
public $enableCsrfValidation = false;
public function actionIndex(){
$data = $this->getBlock()->getLastData();
return $this->render($this->action->id,$data);
}
public function actionAdd(){
$this->enableCsrfValidation = true;
$custom_option = Yii::$app->request->post('custom_option');
......
......@@ -531,6 +531,7 @@ del.my_shop_price {
#footer {
color: #7f7f7f;
overflow-x: hidden;
width:100%;
}
.footer-container {
margin-top: 50px;
......@@ -2371,6 +2372,263 @@ a.next_images {
.shopping-cart-img {
background: rgba(0, 0, 0, 0) url("../images/scart_step1.gif") repeat scroll 0 0;
height: 36px;
margin: 20px auto 5px;
width: 834px;
}
.shopping-cart-div {
position: relative;
}
.data-table {
width: 100%;
}
.data-table thead {
background-color: #f2f2f2;
}
.shopping-cart-div .data-table thead tr {
border: medium none;
}
.shopping-cart-div .data-table tr th {
border: medium none;
}
.shopping-cart-div .data-table tr {
border-bottom: 1px solid #ccc;
}
.shopping-cart-div .data-table tr td {
border: medium none;
}
#shopping-cart-table .a-right {
text-align: center !important;
}
.cartqtydown {
background: rgba(0, 0, 0, 0) url("../images/jj.png") no-repeat scroll -4px -1px;
float: left;
height: 10px;
margin: 12px 4px 0 0;
width: 10px;
}
#shopping-cart-table .qty {
float: left;
text-align: center;
width: 15px !important;
}
a.cartqtyup {
background: rgba(0, 0, 0, 0) url("../images/jj.png") no-repeat scroll -4px -25px;
float: left;
height: 10px;
margin: 12px 0 0 3px;
width: 10px;
}
.cart .cart-collaterals {
padding: 25px 0 0;
}
.btn-remove2 {
background: rgba(0, 0, 0, 0) url("../images/btn_trash.gif") no-repeat scroll 0 0;
display: block;
font-size: 0;
height: 16px;
line-height: 0;
overflow: hidden;
text-indent: -999em;
width: 16px;
}
#discount-coupon-form .discount-form label {
color: #999;
display: block;
font-size: 11px;
font-weight: 100;
margin: 8px 0 10px;
}
#discount-coupon-form .input-box {
float: left;
}
body #discount-coupon-form div.buttons-cou {
float: left;
}
.discount-form .submitbutton {
background: #ccc none repeat scroll 0 0;
border-radius: 0;
font-size: 14px;
height: 31px;
line-height: 31px;
}
.cart .totals {
float: right;
width: 494px;
}
.cart .cart-collaterals .col2-set {
float: left;
width: 400px;
}
.proceed_to_checkout {
margin-top: 35px;
text-align: right;
}
#shopping-cart-totals-table2 {
border-top: 1px solid #333;
font-size: 14px;
margin: 16px 0 0;
}
.cart .totals table th, .cart .totals table td {
padding: 5px;
}
.std b, .std strong {
font-weight: bold;
}
.price {
white-space: nowrap !important;
}
* {
margin: 0;
padding: 0;
}
.std b, .std strong {
font-weight: bold;
}
strong {
font-weight: bold;
}
#shopping-cart-totals-table2 .a-right {
color: #2ab4c4;
font-size: 18px;
}
#shopping-cart-totals-table2 .a-right {
color: #2ab4c4;
font-size: 18px;
}
.proceed_to_checkout {
margin-top: 35px;
text-align: right;
}
.proceed_to_checkout button.button {
display: block;
float: right;
}
a.express_paypal{
background: rgba(0, 0, 0, 0) url("../images/paypaltopay.jpg") repeat scroll 0 0;
display: block;
float: right;
height: 30px;
width: 154px;
}
.proceed_to_checkout .or{
display: block;
float: right;
font-weight: bold;
line-height: 32px;
margin: 0 5px;
}
.proceed_to_checkout button.button span {
font-size: 20px;
}
.cart-collaterals .col2-set .col-2 {
float: left;
}
#discount-coupon-form .discount {
width: 500px;
}
#discount-coupon-form .input-box {
float: left;
}
body #discount-coupon-form div.buttons-cou {
float: left;
}
#discount-coupon-form #coupon_code {
border-radius: 0;
height: 29px;
width: 200px;
}
.discount-form .submitbutton:hover{
background-color:#4c8b99;
}
h2 {
font-size: 18px;
font-weight: normal;
line-height: 1.25;
padding:0 0 2px 0;
}
.a-center {
text-align: center !important;
}
.product-name {
font-size: 1em;
font-weight: normal;
}
.product_info .price_info .no-special {
line-height: 30px;
margin-right: 10px;
color: #cc0000;
font-size: 25px;
}
......
......@@ -46,6 +46,7 @@
<?= Yii::$service->page->widget->render($optionsView,$optionsParam); ?>
</div>
<div class="product_custom_options">
<?php # custom options部分
$optionsView = [
......@@ -53,6 +54,7 @@
'view' => 'catalog/product/index/custom_option.php',
'custom_option' => $custom_option,
'attr_group' => $attr_group,
'product_id' => $_id ,
'middle_img_width' => $media_size['middle_img_width'],
];
$optionsParam = [
......@@ -238,7 +240,7 @@
if(data.status == 'success'){
items_count = data.items_count;
$("#js_cart_items").html(items_count);
alert('add to cart success');
window.location.href="<?= Yii::$service->url->getUrl("checkout/cart") ?>";
}else{
alert('add to cart fail');
}
......@@ -260,6 +262,57 @@
window.location.href = url;
}
});
// 改变个数的时候,价格随之变动
$(".qty").blur(function(){
// 如果全部选择完成,需要到ajax请求,得到最后的价格
i = 1;
$(".product_custom_options .pg .rg ul.required").each(function(){
val = $(this).find("li.current a.current").attr("value");
attr = $(this).find("li.current a.current").attr("attr");
if(!val){
i = 0;
}
});
if(i){
getCOUrl = "<?= Yii::$service->url->getUrl('catalog/product/getcoprice'); ?>";
product_id = "<?= $_id ?>";
qty = $(".qty").val();
custom_option_sku = '';
for(x in custom_option_arr){
one = custom_option_arr[x];
j = 1;
$(".product_custom_options .pg .rg ul.required").each(function(){
val = $(this).find("li.current a.current").attr("value");
attr = $(this).find("li.current a.current").attr("attr");
if(one[attr] != val){
j = 0;
//break;
}
});
if(j){
custom_option_sku = one['sku'];
break;
}
}
$data = {
custom_option_sku:custom_option_sku,
qty:qty,
product_id:product_id
};
jQuery.ajax({
async:true,
timeout: 6000,
dataType: 'json',
type:'get',
data: $data,
url:getCOUrl,
success:function(data, textStatus){
$(".price_info").html(data.price);
},
error:function (XMLHttpRequest, textStatus, errorThrown){}
});
}
});
});
<?php $this->endBlock(); ?>
<?php $this->registerJs($this->blocks['add_to_cart'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
......
......@@ -173,6 +173,52 @@ $(document).ready(function(){
});
});
// 如果全部选择完成,需要到ajax请求,得到最后的价格
i = 1;
$(".product_custom_options .pg .rg ul.required").each(function(){
val = $(this).find("li.current a.current").attr("value");
attr = $(this).find("li.current a.current").attr("attr");
if(!val){
i = 0;
}
});
if(i){
for(x in custom_option_arr){
one = custom_option_arr[x];
j = 1;
$(".product_custom_options .pg .rg ul.required").each(function(){
val = $(this).find("li.current a.current").attr("value");
attr = $(this).find("li.current a.current").attr("attr");
if(one[attr] != val){
j = 0;
//break;
}
});
if(j){
getCOUrl = "<?= Yii::$service->url->getUrl('catalog/product/getcoprice'); ?>";
custom_option_sku = one['sku'];
product_id = "<?= $product_id ?>";
qty = $(".qty").val();
$data = {
custom_option_sku:custom_option_sku,
qty:qty,
product_id:product_id
};
jQuery.ajax({
async:true,
timeout: 6000,
dataType: 'json',
type:'get',
data: $data,
url:getCOUrl,
success:function(data, textStatus){
$(".price_info").html(data.price);
},
error:function (XMLHttpRequest, textStatus, errorThrown){}
});
}
}
}
}
});
......@@ -182,6 +228,7 @@ $(document).ready(function(){
});
<?php $this->endBlock(); ?>
</script>
......
......@@ -9,8 +9,7 @@
</div>
<div class="clear"></div>
<?php }else{ ?>
<div class="price">
<span class="price_symbol"><?= $price_info['price']['symbol'] ?></span>
<span class="price_val"><?= $price_info['price']['value'] ?></span>
<div class="price no-special">
<?= $price_info['price']['symbol'] ?><?= $price_info['price']['value'] ?>
</div>
<?php } ?>
\ No newline at end of file
<div class="main container one-column">
<div class="col-main">
<div class="product_page">
<div class="cart">
<div class="page-title title-buttons">
<div class="shopping-cart-img">
</div>
</div>
<div>
<div class="shopping-cart-div">
<div class="shopping-cart-ab">
</div>
<table id="shopping-cart-table" class="data-table cart-table">
<colgroup>
<col width="1">
<col width="">
<col width="1">
<col width="10">
<col width="1">
<col width="1">
<col width="1">
</colgroup>
<thead>
<tr class="first last">
<th rowspan="1">&nbsp;</th>
<th rowspan="1"><span class="nobr">Product Name</span></th>
<th class="a-center" colspan="1"><span class="nobr">Unit Price</span></th>
<th rowspan="1" class="a-center">Qty</th>
<th class="a-center" colspan="1">Subtotal</th>
<th rowspan="1" class="a-center">&nbsp;</th>
</tr>
</thead>
<tfoot>
</tfoot>
<tbody>
<tr class="first last odd">
<td>
<a href="http://www.intosmile.com/retro-deep-v-neck-irregular-tassels-hem-sleeveless-dress.html" title="Retro Deep V-neck Irregular Tassels Hem Sleeveless Dress" class="product-image">
<img src="http://img.intosmile.com/media/catalog/product/cache/110/110/710aa4d924f51b2be23e7fd5eda0d13f/g/y/gyxh0849onfancy.jpg" alt="2121" width="75" height="75">
</a>
</td>
<td>
<h2 class="product-name">
<a href="http://www.intosmile.com/retro-deep-v-neck-irregular-tassels-hem-sleeveless-dress.html">Retro Deep V-neck Irregular Tassels Hem Sleeveless Dress</a>
</h2>
<ul><li>Color:Black </li><li>Size:S </li></ul>
</td>
<td class="a-right">
<span class="cart-price">
<span class="price">$19.00</span>
</span>
</td>
<td class="a-center">
<div style="width:80px;">
<a href="javascript:void(0)" class="cartqtydown changeitemqty" rel="266551" num="-1"></a>
<input name="cart[qty]" size="4" title="Qty" class="input-text qty" rel="266551" maxlength="12" value="1">
<a href="javascript:void(0)" class="cartqtyup changeitemqty" rel="266551" num="1"></a>
<div class="clear"></div>
</div>
</td>
<td class="a-right">
<span class="cart-price">
<span class="price">$19.00</span>
</span>
</td>
<td class="a-center last">
<a href="http://www.intosmile.com/checkout/cart/remove?item_id=266551&amp;unec=aHR0cDovL3d3dy5pbnRvc21pbGUuY29tL2NoZWNrb3V0L2NhcnQ=" title="Remove item" class="btn-remove btn-remove2">Remove item</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cart-collaterals">
<div class="col2-set">
<div class="col-1">
</div>
<div class="col-2">
<form id="discount-coupon-form" action="http://www.intosmile.com/checkout/cart/couponpost" method="post">
<div class="discount">
<h2>Discount Codes</h2>
<div class="discount-form">
<label for="coupon_code">Enter your coupon code if you have one.</label>
<input name="_csrf" id="csrf_coupone" value="" type="hidden">
<div class="input-box">
<input style="color:#777;" class="input-text" id="coupon_code" name="coupon_code" value="">
</div>
<div class="buttons-cou">
<a href="javascript:void(0)" onclick="cartcouponsubmit()" class="submitbutton"><span><span>Add Coupon</span></span> </a>
</div>
<div class="clear"></div>
</div>
</div>
</form>
</div>
</div>
<div class="totals cart-totals">
<div class="process_total">
<table id="shopping-cart-totals-table">
<colgroup>
<col>
<col width="1">
</colgroup>
<tbody>
<tr>
<td style="" class="a-left" colspan="1">
Item Subtotal: </td>
<td style="" class="a-right">
<span class="price">$19.00</span> </td>
</tr><tr>
<td style="" class="a-left" colspan="1">
Shipping </td>
<td style="" class="a-right">
<span class="price">$0.00</span> </td>
</tr><tr>
<td style="" class="a-left" colspan="1">
Coupon: </td>
<td style="" class="a-right">
<span class="price">-$0.00</span> </td>
</tr>
</tbody>
</table>
<table id="shopping-cart-totals-table2">
<colgroup>
<col>
<col width="90">
</colgroup>
<tbody>
<tr>
<td style="" class="a-left" colspan="1">
<strong>Grand Total</strong>
</td>
<td style="" class="a-right">
<strong><span class="price">$19.00</span></strong>
</td>
</tr>
</tbody>
</table>
</div>
<div class="proceed_to_checkout">
<button onclick="location.href='http://www.intosmile.com/checkout/onepage'" type="button" title="Proceed to Checkout" class="button btn-proceed-checkout btn-checkout"><span><span>Proceed to Pay</span></span></button>
<span class="or">- OR - </span>
<a class="express_paypal" href="<?= Yii::$service->url->getUrl('paypal/express/start'); ?>">
</a>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
\ No newline at end of file
......@@ -63,6 +63,10 @@ class Cart extends Service
}
protected function actionGetCartInfo(){
return Yii::$service->cart->quote->getCartInfo();
}
/**
* 初始化
*/
......
......@@ -101,6 +101,10 @@ class Product extends Service
return true;
}
protected function actionIsActive($status){
return ($status == 1) ? true : false;
}
protected function actionGetCustomOptionAttrInfo($productAttrGroup){
$arr = [];
if($productAttrGroup == $this->_defaultAttrGroup){
......
......@@ -124,6 +124,22 @@ class Quote extends Service
}
public function getCartInfo(){
$cart_id = $this->getCartId();
if(!$cart_id){
return ;
}
$this->getMyCart();
$items_qty = $this->_my_cart['items_count'];
if($items_qty <= 0){
return ;
}
$cart_product = Yii::$service->cart->quoteItem->getCartProductInfo();
}
}
\ No newline at end of file
......@@ -101,6 +101,41 @@ class QuoteItem extends Service
return $item_qty;
}
/**
* 得到当前购物车的产品价格信息。
*/
public function getCartProductInfo(){
$cart_id = Yii::$service->cart->quote->getCartId();
$products = [];
$product_total = 0;
if($cart_id){
$data = MyCartItem::find()->where([
'cart_id' => $cart_id
])->all();
if(is_array($data) && !empty($data)){
foreach($data as $one){
$product_id = $one['product_id'];
$qty = $one['qty'];
$custom_option_sku = $one['custom_option_sku'];
$product_price = Yii::$service->product->price->getCartPriceByProductId($product_id,$qty,$custom_option_sku);
$product_row_price = $product_price * $qty;
$product_total += $product_row_total;
$products[] = [
'product_id' => $product_id ,
'qty' => $qty ,
'custom_option_sku' => $custom_option_sku ,
'product_price' => $product_price ,
'product_row_price' => $product_row_price ,
];
}
return [
'products' => $products,
'product_total' => $product_total,
];
}
}
}
}
\ No newline at end of file
......@@ -77,6 +77,70 @@ class Price extends Service
}
return $return_price;
}
/**
* @property $productId | String
* @property $qty | Int
* @property $custom_option_sku | String
* 通过产品以及个数,custonOptionSku 得到产品的最终价格
*/
protected function actionGetCartPriceByProductId($productId,$qty,$custom_option_sku){
$product = Yii::$service->product->getByPrimaryKey($productId);
$custom_option_price = 0;
$status = isset($product['status']) ? $product['status'] : 0;
if($product['price'] && Yii::$service->product->isActive($status)){
$price = $product['price'];
$special_price = isset($product['special_price']) ? $product['special_price'] : 0;
$special_from = isset($product['special_from']) ? $product['special_from'] : '';
$special_to = isset($product['special_to']) ? $product['special_to'] : '';
$tier_price = isset($product['tier_price']) ? $product['tier_price'] : [];
$custom_option = isset($product['custom_option']) ? $product['custom_option'] : '';
if(!empty($custom_option) && $custom_option_sku && isset($custom_option[$custom_option_sku])){
if($co = $custom_option[$custom_option_sku]){
$custom_option_price = isset($co['price']) ? $co['price'] : 0;
}
}
return $this->getCartPrice(
$price , $special_price,
$special_from , $special_to,
$qty , $custom_option_price,
$tier_price
);
}
}
# 产品加入购物车,得到相应个数的最终价格。
protected function actionGetCartPrice(
$price , $special_price,
$special_from , $special_to,
$qty='' , $custom_option_price,
$tier_price=[] , $format = 1
){
if($this->specialPriceisActive($price,$special_price,$special_from,$special_to)){
$return_price = $special_price;
}else{
$return_price = $price;
}
if($qty > 1){
$return_price = $this->getTierPrice($qty,$tier_price,$return_price);
}
$return_price = $return_price + $custom_option_price;
if($format){
$format_price = $this->formatPrice($return_price);
return $format_price ;
}else{
return $return_price;
}
}
/**
* @property $qty | Int
* @property $price | Float 一个产品的单价(如果有特价,那么这个值是一个产品的特价)
......@@ -93,14 +157,18 @@ class Price extends Service
}
$t_price = $price;
if(is_array($tier_price_arr) && !empty($tier_price_arr)){
foreach($tier_price_arr as $one){
$t_qty = $one['qty'];
if($t_qty <= $qty){
$t_price = $one['price'];
if($t_qty < $qty){
continue;
}else{
return $t_price;
}
$t_price = $one['price'];
}
}
return $t_price;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册