提交 bc54cb26 编写于 作者: T Terry

appfront: 购物车勾选产品切换active状态

上级 ebd0ec87
......@@ -221,4 +221,54 @@ class CartController extends AppfrontController
$innerTransaction->rollBack();
}
}
public function actionSelectone()
{
$item_id = Yii::$app->request->get('item_id');
$checked = Yii::$app->request->get('checked');
$checked = $checked == 1 ? true : false;
$innerTransaction = Yii::$app->db->beginTransaction();
try {
$status = Yii::$service->cart->selectOneItem($item_id, $checked);
if ($status) {
echo json_encode([
'status' => 'success',
]);
$innerTransaction->commit();
} else {
echo json_encode([
'status' => 'fail',
'content' => Yii::$service->helper->errors->get(',')
]);
$innerTransaction->rollBack();
}
} catch (Exception $e) {
$innerTransaction->rollBack();
}
}
public function actionSelectall()
{
$checked = Yii::$app->request->get('checked');
$checked = $checked == 1 ? true : false;
$innerTransaction = Yii::$app->db->beginTransaction();
try {
$status = Yii::$service->cart->selectAllItem($checked);
if ($status) {
echo json_encode([
'status' => 'success',
]);
$innerTransaction->commit();
} else {
echo json_encode([
'status' => 'fail',
'content' => Yii::$service->helper->errors->get(',')
]);
$innerTransaction->rollBack();
}
} catch (Exception $e) {
$innerTransaction->rollBack();
}
}
}
......@@ -68,7 +68,7 @@ use fecshop\app\appfront\helper\Format;
<tr class="first last odd">
<td>
<input <?= ($product_one['active'] == Yii::$service->cart->quoteItem->activeStatus ) ? 'checked="checked"' : '' ?> type="checkbox" name="cart_select_item" class="cart_select cart_select_item">
<input rel="<?= $product_one['item_id']; ?>" <?= ($product_one['active'] == Yii::$service->cart->quoteItem->activeStatus ) ? 'checked="checked"' : '' ?> type="checkbox" name="cart_select_item" class="cart_select cart_select_item">
</td>
......@@ -285,8 +285,31 @@ use fecshop\app\appfront\helper\Format;
// 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') ?>"
// set select all checkbox
selectall = "<?= Yii::$app->request->get('selectall') ?>";
selectAllChecked = false;
if (selectall == 1) {
selectAllChecked = true;
} else {
item_select_all = 1;
$(".cart_select_item").each(function(){
checked = $(this).is(':checked');
if (checked == false) {
item_select_all = 0;
}
});
if (item_select_all == 1) {
selectAllChecked = true;
}
}
$(".cart_select_all").attr("checked",selectAllChecked);
currentUrl = "<?= Yii::$service->url->getUrl('checkout/cart') ?>";
updateCartInfoUrl = "<?= Yii::$service->url->getUrl('checkout/cart/updateinfo') ?>";
selectOneProductUrl = "<?= Yii::$service->url->getUrl('checkout/cart/selectone') ?>";
selectAllProductUrl = "<?= Yii::$service->url->getUrl('checkout/cart/selectall') ?>";
$(".cartqtydown").click(function(){
$item_id = $(this).attr("rel");
num = $(this).attr("num");
......@@ -334,8 +357,8 @@ $(document).ready(function(){
});
});
$(".btn-remove").click(function(){
$(".btn-remove").click(function(){
$item_id = $(this).attr("rel");
$data = {
......@@ -359,6 +382,54 @@ $(document).ready(function(){
});
$(".cart_select_item").click(function(){
$item_id = $(this).attr("rel");
checked = $(this).is(':checked');
checked = checked ? 1 : 0;
$data = {
item_id:$item_id,
checked:checked
};
jQuery.ajax({
async:true,
timeout: 6000,
dataType: 'json',
type:'get',
data: $data,
url:selectOneProductUrl,
success:function(data, textStatus){
if(data.status == 'success'){
window.location.href = currentUrl;
}
},
error:function (XMLHttpRequest, textStatus, errorThrown){}
});
});
$(".cart_select_all").click(function(){
checked = $(this).is(':checked');
checked = checked ? 1 : 0;
$data = {
checked:checked
};
selectCurrentUrl = currentUrl + '?selectall=' + checked;
jQuery.ajax({
async:true,
timeout: 6000,
dataType: 'json',
type:'get',
data: $data,
url:selectAllProductUrl,
success:function(data, textStatus){
if(data.status == 'success'){
window.location.href = selectCurrentUrl;
}
},
error:function (XMLHttpRequest, textStatus, errorThrown){}
});
});
$(".add_coupon_submit").click(function(){
coupon_code = $("#coupon_code").val();
coupon_type = $(".couponType").val();
......
......@@ -141,6 +141,34 @@ class Cart extends Service
return true;
}
/**
* @property $item_id | Int 购物车产品表的id字段
* 通过item id 将购物车中的某个产品的个数加一
*/
protected function actionSelectOneItem($item_id, $checked)
{
$status = Yii::$service->cart->quoteItem->selectOneItem($item_id, $checked);
if (!$status) {
return false;
}
Yii::$service->cart->quote->computeCartInfo();
return true;
}
/**
* @property $item_id | Int 购物车产品表的id字段
* 通过item id 将购物车中的某个产品的个数加一
*/
protected function actionSelectAllItem($checked)
{
$status = Yii::$service->cart->quoteItem->selectAllItem($checked);
if (!$status) {
return false;
}
Yii::$service->cart->quote->computeCartInfo();
return true;
}
/**
* 购物车合并:对应的是用户登录前后购物车的合并
......
......@@ -369,6 +369,70 @@ class QuoteItem extends Service
return false;
}
/**
* @property $item_id | Int , quoteItem表的id
* @return bool
* 将这个item_id对应的产品个数+1.
*/
public function selectOneItem($item_id, $checked)
{
$cart_id = Yii::$service->cart->quote->getCartId();
if ($cart_id) {
$one = $this->_itemModel->find()->where([
'cart_id' => $cart_id,
'item_id' => $item_id,
])->one();
$product_id = $one['product_id'];
if ($one['item_id'] && $product_id) {
//$product = Yii::$service->product->getByPrimaryKey($product_id);
//$changeQty = Yii::$service->cart->getCartQty($product['package_number'], 1);
//$one['qty'] = $one['qty'] + $changeQty;
if ($checked == true) {
$one->active = $this->activeStatus;
} else {
$one->active = $this->noActiveStatus;
}
$one->save();
// 重新计算购物车的数量
Yii::$service->cart->quote->computeCartInfo();
return true;
}
}
return false;
}
/**
* @property $item_id | Int , quoteItem表的id
* @return bool
* 将这个item_id对应的产品个数+1.
*/
public function selectAllItem($checked)
{
$cart_id = Yii::$service->cart->quote->getCartId();
if ($cart_id) {
$active = $this->noActiveStatus;
if ($checked == true) {
$active = $this->activeStatus;
}
$updateCount = $this->_itemModel->updateAll(
['active' => $active],
['cart_id' => $cart_id]
);
if ($updateCount > 0) {
Yii::$service->cart->quote->computeCartInfo();
}
return true;
}
return false;
}
/**
* @property $cart_id | int 购物车id
* 删除购物车中的所有的active产品。对于noActive产品保留
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册