meraproject/module/shop/cart/controller.php
keboss-m 5c21d25d45 Initial commit: Merakomis portal, Docker stack and user-reader API.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 11:04:05 +03:00

135 lines
3.9 KiB
PHP

<?
namespace controller\shop\cart;
use shop\Cart;
use shop\core\structure\shopControllerTable;
use shop\cart\Item as ShopItem;
use shop\Coupon;
class Api extends shopControllerTable {
static $class = 'shop\Cart';
static function getMyCartID(){
echo Cart::getMyCartID();
}
static function clearMy(){
Cart::clearMy();
}
static function setMyCount(){
$res = ['id'=>intval($_POST['id'])];
$CART = Cart::getMyCartData();
if(in_array($_POST['id'],array_keys($CART['citems']))){
if ($count = floatval($_POST['count'])) {
Cart::setShopItemCount($_POST['id'], $count);
}
}
echo je($res);
}
static function removeCouponFromMyCart(){
Cart::updateById(
Cart::getMyCartID(),
[
Cart::$COUPON_NAME => '',
Cart::$COUPON_VALUE => 0,
Cart::$COUPON_TYPE => 0,
]
);
}
static function addCouponToMyCart(){
$res = false;
$shop_id = intval($_POST['shop_id']);
$coupon_name = trim($_POST['coupon']);
if($shop_id && $coupon_name) {
$a = Coupon::select([
\Query::COUNT => 1,
\Query::WHERE => new \Where(\Where::_and([
\Where::_operator(Coupon::$SHOP, '=', $shop_id),
\Where::_operator(Coupon::$NAME, '=', $coupon_name),
]))
],true);
if($a){
$coupon = array_pop($a);
$res = true;
Cart::updateById(
Cart::getMyCartID(),
[
Cart::$COUPON_NAME => $coupon[Coupon::$NAME],
Cart::$COUPON_VALUE => $coupon[Coupon::$VALUE],
Cart::$COUPON_TYPE => $coupon[Coupon::$TYPE],
]
);
}
}
echo je(['result'=>$res]);
}
static function removeMyShopItem(){
$res = ['id'=>intval($_POST['id'])];
$CART = Cart::getMyCartData();
if(in_array($_POST['id'],array_keys($CART['citems']))){
Cart::removeShopItem($_POST['id']);
}
echo je($res);
}
static function getMyCartTotals(){
$CART = Cart::getMyCartData();
$res = [
'citems' => [],
'total' => $CART['total'],
'count' => $CART['count'],
];
foreach ($CART['citems'] as $k=>$v){
$res['citems'][$v[ShopItem::$ID]] = $v[ShopItem::$COUNT] * $v[ShopItem::$PRICE] ;
}
echo je($res);
}
static function addToMyCart(){
$count = floatval($_POST['count']);
$id = intval($_POST['id']);
$props = $_POST['props'];
$shop_id = intval($_POST['shop_id']);
$growth_id = intval($_POST['growth_id']);
$component_count = floatval($_POST['component_count']);
$variant = intval($_POST['variant']);
$component_price_id = intval($_POST['component_price_id']);
$r = Cart::addToCart(
Cart::getMyCartID(),
$shop_id,
$id,
$count,
[
'props' => $props,
'component_count' => $component_count,
'variant' => $variant,
'component_price_id' => $component_price_id,
'data' => $_POST['data'],
],
$growth_id
);
if(isset($_POST['price'])){
$price = cfloatval($_POST['price']);
if($price){
ShopItem::updateById($r->id,[
ShopItem::$PRICE=>$price,
]);
}
}
echo je([
'citem'=>['id'=>$r->id],
]);
}
}