|
@@ -8,6 +8,7 @@ use Longyi\FreeGift\Config\Gift;
|
|
|
use Longyi\FreeGift\Repositories\FreeGiftItemRepository;
|
|
use Longyi\FreeGift\Repositories\FreeGiftItemRepository;
|
|
|
use Longyi\FreeGift\Repositories\FreeGiftRuleRepository;
|
|
use Longyi\FreeGift\Repositories\FreeGiftRuleRepository;
|
|
|
use Longyi\FreeGift\Services\GiftProcessor;
|
|
use Longyi\FreeGift\Services\GiftProcessor;
|
|
|
|
|
+use Longyi\FreeGift\State\GiftCartContext;
|
|
|
use Webkul\Checkout\Facades\Cart;
|
|
use Webkul\Checkout\Facades\Cart;
|
|
|
use Webkul\Product\ProductImage;
|
|
use Webkul\Product\ProductImage;
|
|
|
use Webkul\Product\Repositories\ProductRepository;
|
|
use Webkul\Product\Repositories\ProductRepository;
|
|
@@ -24,9 +25,25 @@ class GiftController extends Controller
|
|
|
protected FreeGiftRuleRepository $giftRuleRepository,
|
|
protected FreeGiftRuleRepository $giftRuleRepository,
|
|
|
protected ProductRepository $productRepository,
|
|
protected ProductRepository $productRepository,
|
|
|
protected ProductImage $productImage,
|
|
protected ProductImage $productImage,
|
|
|
- protected GiftProcessor $processor
|
|
|
|
|
|
|
+ protected GiftProcessor $processor,
|
|
|
|
|
+ protected GiftCartContext $cartContext
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 定位当前购物车。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 本控制器对 Blade 前台(session)和 headless 前台(Authorization: Bearer,
|
|
|
|
|
+ * 游客车的 token 就是 cart id)两种入口都要能用:
|
|
|
|
|
+ * - token 场景下 Cart::getCart() 拿不到车(Cart::initCart() 只看登录客户 /
|
|
|
|
|
+ * session('cart')),只调它会让 /api/free-gift/* 静默返回空;
|
|
|
|
|
+ * - GiftCartContext 与 GraphQL 的 GiftCartProvider 用同一套口径,且在命中
|
|
|
|
|
+ * 时会 Cart::setCart(),后续 Cart::removeItem() 等才能作用于这台车。
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function resolveCart(): ?object
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->cartContext->resolveCart() ?: Cart::getCart();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 当前购物车的赠品候选(弹窗 / Banner 数据源)。
|
|
* 当前购物车的赠品候选(弹窗 / Banner 数据源)。
|
|
|
*/
|
|
*/
|
|
@@ -37,7 +54,7 @@ class GiftController extends Controller
|
|
|
'banners' => [],
|
|
'banners' => [],
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- if (! Cart::getCart()) {
|
|
|
|
|
|
|
+ if (! $this->resolveCart()) {
|
|
|
return new JsonResponse(['data' => $payload]);
|
|
return new JsonResponse(['data' => $payload]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -136,11 +153,11 @@ class GiftController extends Controller
|
|
|
'qty' => ['nullable', 'numeric', 'min:1'],
|
|
'qty' => ['nullable', 'numeric', 'min:1'],
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $cart = Cart::getCart();
|
|
|
|
|
|
|
+ $cart = $this->resolveCart();
|
|
|
|
|
|
|
|
if (! $cart) {
|
|
if (! $cart) {
|
|
|
return new JsonResponse([
|
|
return new JsonResponse([
|
|
|
- 'message' => trans('free_gift::app.cart.gift-rule-inactive'),
|
|
|
|
|
|
|
+ 'message' => trans('free_gift::app.cart.cart-not-found'),
|
|
|
], 422);
|
|
], 422);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -174,6 +191,17 @@ class GiftController extends Controller
|
|
|
'cart_item_id' => ['required', 'integer'],
|
|
'cart_item_id' => ['required', 'integer'],
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 先定位当前车(同时 Cart::setCart()):Cart::removeItem() 用的是
|
|
|
|
|
+ * $this->cart,token 场景下它本来为空,会直接 return false —— 表现为
|
|
|
|
|
+ * 「请求成功但什么都没删」。
|
|
|
|
|
+ */
|
|
|
|
|
+ if (! $this->resolveCart()) {
|
|
|
|
|
+ return new JsonResponse([
|
|
|
|
|
+ 'message' => trans('free_gift::app.cart.cart-not-found'),
|
|
|
|
|
+ ], 422);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Cart::removeItem((int) $data['cart_item_id']);
|
|
Cart::removeItem((int) $data['cart_item_id']);
|
|
|
|
|
|
|
|
Cart::collectTotals();
|
|
Cart::collectTotals();
|