Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
yii2_fecshop
提交
cbee0a91
Y
yii2_fecshop
项目概览
OpenDocCN
/
yii2_fecshop
通知
17
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yii2_fecshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
cbee0a91
编写于
11月 30, 2016
作者:
R
root
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
购物车页面添加功能:产品个数增减,产品item删除
上级
dadf6cbb
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
299 addition
and
25 deletion
+299
-25
app/appfront/helper/Format.php
app/appfront/helper/Format.php
+37
-0
app/appfront/modules/Checkout/block/cart/Index.php
app/appfront/modules/Checkout/block/cart/Index.php
+10
-1
app/appfront/modules/Checkout/controllers/CartController.php
app/appfront/modules/Checkout/controllers/CartController.php
+24
-0
app/appfront/theme/base/front/checkout/cart/index.php
app/appfront/theme/base/front/checkout/cart/index.php
+112
-16
services/Cart.php
services/Cart.php
+63
-8
services/cart/QuoteItem.php
services/cart/QuoteItem.php
+53
-0
未找到文件。
app/appfront/helper/Format.php
0 → 100644
浏览文件 @
cbee0a91
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace
fecshop\app\appfront\helper
;
use
Yii
;
use
fec\helpers\CConfig
;
use
yii\base\InvalidValueException
;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class
Format
{
public
static
function
price
(
$price
){
return
number_format
(
$price
,
2
);
}
}
\ No newline at end of file
app/appfront/modules/Checkout/block/cart/Index.php
浏览文件 @
cbee0a91
...
...
@@ -54,7 +54,16 @@ class Index {
$custom_option
=
isset
(
$product_one
[
'custom_option'
])
?
$product_one
[
'custom_option'
]
:
''
;
$custom_option_sku
=
$product_one
[
'custom_option_sku'
];
if
(
isset
(
$custom_option
[
$custom_option_sku
])
&&
!
empty
(
$custom_option
[
$custom_option_sku
])){
$cart_info
[
'products'
][
$k
][
'custom_option_info'
]
=
$custom_option
[
$custom_option_sku
];
$custom_option_info
=
$custom_option
[
$custom_option_sku
];
$custom_option_info_arr
=
[];
foreach
(
$custom_option_info
as
$attr
=>
$val
){
if
(
!
in_array
(
$attr
,[
'qty'
,
'sku'
,
'price'
,
'image'
])){
$attr
=
str_replace
(
'_'
,
' '
,
$attr
);
$attr
=
ucfirst
(
$attr
);
$custom_option_info_arr
[
$attr
]
=
$val
;
}
}
$cart_info
[
'products'
][
$k
][
'custom_option_info'
]
=
$custom_option_info_arr
;
$custom_option_image
=
isset
(
$custom_option
[
$custom_option_sku
][
'image'
])
?
$custom_option
[
$custom_option_sku
][
'image'
]
:
''
;
if
(
$custom_option_image
){
$cart_info
[
'products'
][
$k
][
'image'
]
=
$custom_option_image
;
...
...
app/appfront/modules/Checkout/controllers/CartController.php
浏览文件 @
cbee0a91
...
...
@@ -56,6 +56,30 @@ class CartController extends AppfrontController
}
exit
;
}
public
function
actionUpdateinfo
(){
$item_id
=
Yii
::
$app
->
request
->
get
(
'item_id'
);
$up_type
=
Yii
::
$app
->
request
->
get
(
'up_type'
);
if
(
$up_type
==
'add_one'
){
$status
=
Yii
::
$service
->
cart
->
addOneItem
(
$item_id
);
}
else
if
(
$up_type
==
'less_one'
){
$status
=
Yii
::
$service
->
cart
->
lessOneItem
(
$item_id
);
}
else
if
(
$up_type
==
'remove'
){
$status
=
Yii
::
$service
->
cart
->
removeItem
(
$item_id
);
}
if
(
$status
){
echo
json_encode
([
'status'
=>
'success'
]);
}
else
{
echo
json_encode
([
'status'
=>
'fail'
]);
}
}
}
...
...
app/appfront/theme/base/front/checkout/cart/index.php
浏览文件 @
cbee0a91
<?php
use
fecshop\app\appfront\helper\Format
;
?>
<div
class=
"main container one-column"
>
<div
class=
"col-main"
>
<?php
if
(
is_array
(
$cart_info
)
&&
!
empty
(
$cart_info
)){
?>
<div
class=
"product_page"
>
<div
class=
"cart"
>
...
...
@@ -8,8 +13,7 @@
</div>
</div>
<?php
if
(
is_array
(
$cart_info
)
&&
!
empty
(
$cart_info
)){
?>
<div>
<div>
<?php
if
(
is_array
(
$cart_info
[
'products'
])
&&
(
!
empty
(
$cart_info
[
'products'
]))){
?>
<div
class=
"shopping-cart-div"
>
...
...
@@ -56,9 +60,9 @@
<?php
if
(
is_array
(
$product_one
[
'custom_option_info'
])){
?>
<ul>
<?php
foreach
(
$product_one
[
'custom_option_info'
]
as
$label
=>
$val
){
?>
<?php
if
(
!
in_array
(
$label
,[
'qty'
,
'sku'
,
'price'
,
'image'
])){
?>
<li>
<?=
$label
?>
:
<?=
$val
?>
</li>
<?php
}
?>
<?php
}
?>
</ul>
<?php
}
?>
...
...
@@ -67,16 +71,16 @@
<td
class=
"a-right"
>
<span
class=
"cart-price"
>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
$product_one
[
'product_price'
]
;
?>
</span>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$product_one
[
'product_price'
])
;
?>
</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=
"
<?=
$product_one
[
'qty'
];
?>
"
>
<a
href=
"javascript:void(0)"
class=
"cartqtyup changeitemqty"
rel=
"
266551"
num=
"1
"
></a>
<a
href=
"javascript:void(0)"
class=
"cartqtydown changeitemqty"
rel=
"
<?=
$product_one
[
'item_id'
];
?>
"
num=
"
<?=
$product_one
[
'qty'
];
?>
"
></a>
<input
name=
"cart[qty]"
size=
"4"
title=
"Qty"
class=
"input-text qty"
rel=
"
<?=
$product_one
[
'item_id'
];
?>
"
maxlength=
"12"
value=
"
<?=
$product_one
[
'qty'
];
?>
"
>
<a
href=
"javascript:void(0)"
class=
"cartqtyup changeitemqty"
rel=
"
<?=
$product_one
[
'item_id'
];
?>
"
num=
"
<?=
$product_one
[
'qty'
];
?>
"
></a>
<div
class=
"clear"
></div>
</div>
</td>
...
...
@@ -84,11 +88,11 @@
<td
class=
"a-right"
>
<span
class=
"cart-price"
>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
$product_one
[
'product_row_price'
]
;
?>
</span>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$product_one
[
'product_row_price'
])
;
?>
</span>
</span>
</td>
<td
class=
"a-center last"
>
<a
href=
"
http://www.intosmile.com/checkout/cart/remove?item_id=266551&unec=aHR0cDovL3d3dy5pbnRvc21pbGUuY29tL2NoZWNrb3V0L2NhcnQ=
"
title=
"Remove item"
class=
"btn-remove btn-remove2"
>
Remove item
</a>
<a
href=
"
javascript:void(0)"
rel=
"
<?=
$product_one
[
'item_id'
];
?>
"
title=
"Remove item"
class=
"btn-remove btn-remove2"
>
Remove item
</a>
</td>
</tr>
<?php
}
?>
...
...
@@ -137,17 +141,17 @@
<td
style=
""
class=
"a-left"
colspan=
"1"
>
Item Subtotal:
</td>
<td
style=
""
class=
"a-right"
>
<span
class=
"price"
>
$19.00
</span>
</td>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$cart_info
[
'product_total'
]);
?>
</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>
<span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$cart_info
[
'shipping_cost'
]);
?>
</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>
<span
class=
"price"
>
-
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$cart_info
[
'coupon_cost'
]);
?>
</span>
</td>
</tr>
</tbody>
</table>
...
...
@@ -162,7 +166,7 @@
<strong>
Grand Total
</strong>
</td>
<td
style=
""
class=
"a-right"
>
<strong><span
class=
"price"
>
$19.00
</span></strong>
<strong><span
class=
"price"
>
<?=
$currency_info
[
'symbol'
];
?><?=
Format
::
price
(
$cart_info
[
'grand_total'
])
?>
</span></strong>
</td>
</tr>
</tbody>
...
...
@@ -180,8 +184,100 @@
</div>
<div
class=
"clear"
></div>
</div>
<?php
}
?>
</div>
</div>
<?php
}
else
{
?>
<div
class=
"empty_cart"
>
Your Cart is empty, You Can
<a
rel=
"nofollow"
href=
"
<?=
Yii
::
$service
->
url
->
homeUrl
()
?>
"
>
Click Here to Home Page
</a>
</div>
<?php
}
?>
</div>
</div>
\ No newline at end of file
</div>
<script>
// add to cart js
<?php
$this
->
beginBlock
(
'changeCartInfo'
)
?>
$
(
document
).
ready
(
function
(){
currentUrl
=
"
<?=
Yii
::
$service
->
url
->
getUrl
(
'checkout/cart'
)
?>
"
updateCartInfoUrl
=
"
<?=
Yii
::
$service
->
url
->
getUrl
(
'checkout/cart/updateinfo'
)
?>
"
$
(
"
.cartqtydown
"
).
click
(
function
(){
$item_id
=
$
(
this
).
attr
(
"
rel
"
);
num
=
$
(
this
).
attr
(
"
num
"
);
if
(
num
>
1
){
$data
=
{
item_id
:
$item_id
,
up_type
:
"
less_one
"
};
jQuery
.
ajax
({
async
:
true
,
timeout
:
6000
,
dataType
:
'
json
'
,
type
:
'
get
'
,
data
:
$data
,
url
:
updateCartInfoUrl
,
success
:
function
(
data
,
textStatus
){
if
(
data
.
status
==
'
success
'
){
window
.
location
.
href
=
currentUrl
;
}
},
error
:
function
(
XMLHttpRequest
,
textStatus
,
errorThrown
){}
});
}
});
$
(
"
.cartqtyup
"
).
click
(
function
(){
$item_id
=
$
(
this
).
attr
(
"
rel
"
);
$data
=
{
item_id
:
$item_id
,
up_type
:
"
add_one
"
};
jQuery
.
ajax
({
async
:
true
,
timeout
:
6000
,
dataType
:
'
json
'
,
type
:
'
get
'
,
data
:
$data
,
url
:
updateCartInfoUrl
,
success
:
function
(
data
,
textStatus
){
if
(
data
.
status
==
'
success
'
){
window
.
location
.
href
=
currentUrl
;
}
},
error
:
function
(
XMLHttpRequest
,
textStatus
,
errorThrown
){}
});
});
$
(
"
.btn-remove
"
).
click
(
function
(){
$item_id
=
$
(
this
).
attr
(
"
rel
"
);
$data
=
{
item_id
:
$item_id
,
up_type
:
"
remove
"
};
jQuery
.
ajax
({
async
:
true
,
timeout
:
6000
,
dataType
:
'
json
'
,
type
:
'
get
'
,
data
:
$data
,
url
:
updateCartInfoUrl
,
success
:
function
(
data
,
textStatus
){
if
(
data
.
status
==
'
success
'
){
window
.
location
.
href
=
currentUrl
;
}
},
error
:
function
(
XMLHttpRequest
,
textStatus
,
errorThrown
){}
});
});
});
<?php
$this
->
endBlock
();
?>
<?php
$this
->
registerJs
(
$this
->
blocks
[
'changeCartInfo'
],
\
yii\web\View
::
POS_END
);
//将编写的js代码注册到页面底部 ?>
</
script
>
\ No newline at end of file
services/Cart.php
浏览文件 @
cbee0a91
...
...
@@ -96,7 +96,69 @@ class Cart extends Service
}
*/
/**
* @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
();
$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
;
}
protected
function
actionGetUserCartInfo
(){
...
...
@@ -164,14 +226,7 @@ class Cart extends Service
}
/**
* $item_ids
* remove cart items by $items_ids
*/
protected
function
actionRemoveItems
(
$item_ids
){
}
/**
* clear cart product.
...
...
services/cart/QuoteItem.php
浏览文件 @
cbee0a91
...
...
@@ -132,6 +132,7 @@ class QuoteItem extends Service
//$product_row_price['symbol'] = $product_price['symbol'];
$product_total
+=
$product_row_price
;
$products
[]
=
[
'item_id'
=>
$one
[
'item_id'
],
'product_id'
=>
$product_id
,
'qty'
=>
$qty
,
'custom_option_sku'
=>
$custom_option_sku
,
...
...
@@ -153,6 +154,58 @@ class QuoteItem extends Service
}
}
public
function
addOneItem
(
$item_id
){
$cart_id
=
Yii
::
$service
->
cart
->
quote
->
getCartId
();
if
(
$cart_id
){
$one
=
MyCartItem
::
find
()
->
where
([
'cart_id'
=>
$cart_id
,
'item_id'
=>
$item_id
,
])
->
one
();
if
(
$one
[
'item_id'
]){
$one
[
'qty'
]
=
$one
[
'qty'
]
+
1
;
$one
->
save
();
return
true
;
}
}
return
false
;
}
public
function
lessOneItem
(
$item_id
){
$cart_id
=
Yii
::
$service
->
cart
->
quote
->
getCartId
();
if
(
$cart_id
){
$one
=
MyCartItem
::
find
()
->
where
([
'cart_id'
=>
$cart_id
,
'item_id'
=>
$item_id
,
])
->
one
();
if
(
$one
[
'item_id'
]){
if
(
$one
[
'qty'
]
>
1
){
$one
[
'qty'
]
=
$one
[
'qty'
]
-
1
;
$one
->
save
();
return
true
;
}
}
}
return
false
;
}
public
function
removeItem
(
$item_id
){
$cart_id
=
Yii
::
$service
->
cart
->
quote
->
getCartId
();
if
(
$cart_id
){
$one
=
MyCartItem
::
find
()
->
where
([
'cart_id'
=>
$cart_id
,
'item_id'
=>
$item_id
,
])
->
one
();
if
(
$one
[
'item_id'
]){
$one
->
delete
();
return
true
;
}
}
return
false
;
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录