| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- namespace Webkul\BagistoApi\Dto;
- use ApiPlatform\Metadata\ApiProperty;
- use Symfony\Component\Serializer\Annotation\Groups;
- /**
- * Shopping cart data transfer object
- */
- class CartData
- {
- #[Groups(['query', 'mutation'])]
- public ?int $id = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Cart token for guest users')]
- public ?string $cartToken = null;
- #[Groups(['query', 'mutation'])]
- public ?int $customerId = null;
- #[Groups(['query', 'mutation'])]
- public ?int $channelId = null;
- #[Groups(['query', 'mutation'])]
- public ?int $itemsCount = null;
- /**
- * Individual cart items - array of CartItemData DTO objects
- *
- * @var array<CartItemData>|null
- */
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Individual cart items')]
- public ?array $items = [];
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Subtotal before discounts and taxes')]
- public ?float $subtotal = null;
- #[Groups(['query', 'mutation'])]
- public ?float $baseSubtotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Total discount amount')]
- public ?float $discountAmount = null;
- #[Groups(['query', 'mutation'])]
- public ?float $baseDiscountAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Total tax amount')]
- public ?float $taxAmount = null;
- #[Groups(['query', 'mutation'])]
- public ?float $baseTaxAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping cost')]
- public ?float $shippingAmount = null;
- #[Groups(['query', 'mutation'])]
- public ?float $baseShippingAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Grand total')]
- public ?float $grandTotal = null;
- #[Groups(['query', 'mutation'])]
- public ?float $baseGrandTotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted subtotal price')]
- public ?string $formattedSubtotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted discount amount')]
- public ?string $formattedDiscountAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted tax amount')]
- public ?string $formattedTaxAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted shipping amount')]
- public ?string $formattedShippingAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted grand total price')]
- public ?string $formattedGrandTotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Applied coupon code')]
- public ?string $couponCode = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Applied gift card number')]
- public ?string $giftcardNumber = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Gift card discount amount')]
- public ?float $giftcardAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base gift card discount amount')]
- public ?float $baseGiftcardAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted gift card discount amount')]
- public ?string $formattedGiftcardAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Remaining gift card balance after use')]
- public ?float $remainingGiftcardAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted remaining gift card balance')]
- public ?string $formattedRemainingGiftcardAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'VIP member discount amount')]
- public ?float $vipPlusAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base VIP member discount amount')]
- public ?float $baseVipPlusAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted VIP member discount amount')]
- public ?string $formattedVipPlusAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'VIP discount amount (5%)')]
- public ?float $vipDiscountAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base VIP discount amount (5%)')]
- public ?float $baseVipDiscountAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted VIP discount amount (5%)')]
- public ?string $formattedVipDiscountAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Whether the current customer is a VIP member')]
- public ?bool $isVip = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'VIP Plus expiration date')]
- public ?string $vipExpireDate = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Whether shipping insurance is used')]
- public ?bool $useShippingInsurance = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Whether shipping insurance is enabled in admin')]
- public ?bool $shippingInsuranceEnabled = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping insurance name')]
- public ?string $shippingInsuranceName = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping insurance type (fixed|percentage)')]
- public ?string $shippingInsuranceType = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping insurance value (percentage or fixed amount)')]
- public ?float $shippingInsuranceValue = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping insurance amount')]
- public ?float $shippingInsuranceAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base shipping insurance amount')]
- public ?float $baseShippingInsuranceAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted shipping insurance amount')]
- public ?string $formattedShippingInsuranceAmount = null;
- #[Groups(['query', 'mutation'])]
- public ?bool $success = null;
- #[Groups(['query', 'mutation'])]
- public ?string $message = null;
- #[Groups(['query', 'mutation'])]
- public ?array $carts = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Unique session token for guest cart')]
- public ?string $sessionToken = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Is this a guest cart')]
- public bool $isGuest = false;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Total quantity of all items')]
- public ?int $itemsQty = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: "Customer's current reward points balance")]
- public int $rewardPoints = 0;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Reward points that can be earned from the current cart')]
- public int $earnableRewardPoints = 0;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Reward points redeemed against the current cart')]
- public int $rewardPointsUsed = 0;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Reward points discount amount')]
- public ?float $rewardPointsAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base reward points discount amount')]
- public ?float $baseRewardPointsAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted reward points discount amount')]
- public ?string $formattedRewardPointsAmount = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Subtotal including tax')]
- public ?float $subTotalInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base subtotal including tax')]
- public ?float $baseSubTotalInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted subtotal including tax')]
- public ?string $formattedSubTotalInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Tax total')]
- public ?float $taxTotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted tax total')]
- public ?string $formattedTaxTotal = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping amount including tax')]
- public ?float $shippingAmountInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Base shipping amount including tax')]
- public ?float $baseShippingAmountInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Formatted shipping amount including tax')]
- public ?string $formattedShippingAmountInclTax = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Billing address')]
- public ?array $billingAddress = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Shipping address')]
- public ?array $shippingAddress = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Applied taxes')]
- public ?array $appliedTaxes = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Has stockable items')]
- public ?bool $haveStockableItems = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Payment method code')]
- public ?string $paymentMethod = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Payment method title')]
- public ?string $paymentMethodTitle = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(readable: true, writable: false, description: 'Selected shipping rate')]
- public ?string $selectedShippingRate = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(readable: true, writable: false, description: 'Selected shipping rate title')]
- public ?string $selectedShippingRateTitle = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Payment redirect URL (if payment gateway redirect needed)')]
- public ?string $paymentRedirectUrl = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Order ID after order creation')]
- public ?string $orderId = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Redirect URL for buy now checkout')]
- public ?string $redirectUri = null;
- public function getSelectedShippingRate(): ?string
- {
- return $this->selectedShippingRate;
- }
- public function setSelectedShippingRate(?string $selectedShippingRate): void
- {
- $this->selectedShippingRate = $selectedShippingRate;
- }
- public function getSelectedShippingRateTitle(): ?string
- {
- return $this->selectedShippingRateTitle;
- }
- public function setSelectedShippingRateTitle(?string $selectedShippingRateTitle): void
- {
- $this->selectedShippingRateTitle = $selectedShippingRateTitle;
- }
- public static function fromModel(\Webkul\Checkout\Models\Cart $cart): self
- {
- $data = new self;
- $data->id = $cart->id;
- $data->cartToken = (string) $cart->id;
- $data->customerId = $cart->customer_id;
- $data->isGuest = ! $cart->customer_id;
- $data->channelId = $cart->channel_id;
- $data->itemsCount = $cart->items()->count();
- $data->itemsQty = (int) ($cart->items_qty ?? 0);
- $items = $cart->items()
- ->with(['product'])
- ->get()
- ->map(fn ($item) => CartItemData::fromModel($item));
- $data->items = $items->toArray();
- $data->subtotal = (float) core()->convertPrice($cart->base_sub_total ?? 0);
- $data->baseSubtotal = (float) ($cart->base_sub_total ?? 0);
- $data->formattedSubtotal = core()->currency($cart->base_sub_total ?? 0);
- $data->subTotalInclTax = (float) core()->convertPrice($cart->base_sub_total_incl_tax ?? $cart->base_sub_total ?? 0);
- $data->baseSubTotalInclTax = (float) ($cart->base_sub_total_incl_tax ?? $cart->base_sub_total ?? 0);
- $data->formattedSubTotalInclTax = core()->currency($cart->base_sub_total_incl_tax ?? $cart->base_sub_total ?? 0);
- $data->taxAmount = (float) core()->convertPrice($cart->base_tax_amount ?? 0);
- $data->baseTaxAmount = (float) ($cart->base_tax_amount ?? 0);
- $data->taxTotal = (float) core()->convertPrice($cart->base_tax_total ?? $cart->base_tax_amount ?? 0);
- $data->formattedTaxTotal = core()->currency($cart->base_tax_total ?? $cart->base_tax_amount ?? 0);
- $data->formattedTaxAmount = core()->currency($cart->base_tax_amount ?? 0);
- $data->discountAmount = (float) core()->convertPrice($cart->base_discount_amount ?? 0);
- $data->baseDiscountAmount = (float) ($cart->base_discount_amount ?? 0);
- $data->formattedDiscountAmount = core()->currency($cart->base_discount_amount ?? 0);
- $data->shippingAmount = (float) core()->convertPrice($cart->base_shipping_amount ?? 0);
- $data->baseShippingAmount = (float) ($cart->base_shipping_amount ?? 0);
- $data->formattedShippingAmount = core()->currency($cart->base_shipping_amount ?? 0);
- $data->shippingAmountInclTax = (float) core()->convertPrice($cart->base_shipping_amount_incl_tax ?? $cart->base_shipping_amount ?? 0);
- $data->baseShippingAmountInclTax = (float) ($cart->base_shipping_amount_incl_tax ?? $cart->base_shipping_amount ?? 0);
- $data->formattedShippingAmountInclTax = core()->currency($cart->base_shipping_amount_incl_tax ?? $cart->base_shipping_amount ?? 0);
- $data->grandTotal = (float) core()->convertPrice($cart->base_grand_total ?? 0);
- $data->baseGrandTotal = (float) ($cart->base_grand_total ?? 0);
- $data->formattedGrandTotal = core()->currency($cart->base_grand_total ?? 0);
- $data->couponCode = $cart->coupon_code;
- // Gift card
- $data->giftcardNumber = $cart->giftcard_number ?? null;
- $data->giftcardAmount = (float) ($cart->giftcard_amount ?? 0);
- $data->baseGiftcardAmount = (float) ($cart->base_giftcard_amount ?? 0);
- $data->formattedGiftcardAmount = core()->currency($cart->base_giftcard_amount ?? 0);
- $data->remainingGiftcardAmount = (float) ($cart->remaining_giftcard_amount ?? 0);
- $data->formattedRemainingGiftcardAmount = core()->formatPrice($cart->remaining_giftcard_amount ?? 0);
- // VIP member discount
- $data->vipPlusAmount = (float) ($cart->vip_plus_amount ?? 0);
- $data->baseVipPlusAmount = (float) ($cart->base_vip_plus_amount ?? 0);
- $data->formattedVipPlusAmount = core()->currency($cart->base_vip_plus_amount ?? 0);
- // VIP discount (5%)
- $data->vipDiscountAmount = (float) ($cart->vip_discount_amount ?? 0);
- $data->baseVipDiscountAmount = (float) ($cart->base_vip_discount_amount ?? 0);
- $data->formattedVipDiscountAmount = core()->currency($cart->base_vip_discount_amount ?? 0);
- // VIP status
- $data->isVip = false;
- $data->vipExpireDate = null;
- if ($cart->customer_id) {
- $customer = \Webkul\Customer\Models\Customer::find($cart->customer_id);
- if ($customer && $customer->vip_expire_date) {
- $expireDate = \Carbon\Carbon::parse($customer->vip_expire_date);
- $data->isVip = !$expireDate->isPast();
- $data->vipExpireDate = $expireDate->toIso8601String();
- }
- }
- // Shipping insurance
- $data->useShippingInsurance = (bool) ($cart->use_shipping_insurance ?? false);
- $data->shippingInsuranceEnabled = (bool) ($cart->shipping_insurance_enabled ?? false);
- $data->shippingInsuranceName = $cart->shipping_insurance_name ?? null;
- $data->shippingInsuranceType = $cart->shipping_insurance_type ?? null;
- $data->shippingInsuranceValue = (float) ($cart->shipping_insurance_value ?? 0);
- $data->shippingInsuranceAmount = (float) ($cart->shipping_insurance_amount ?? 0);
- $data->baseShippingInsuranceAmount = (float) ($cart->base_shipping_insurance_amount ?? 0);
- $data->formattedShippingInsuranceAmount = core()->currency($cart->base_shipping_insurance_amount ?? 0);
- if ($cart->billing_address) {
- $data->billingAddress = [
- 'id' => $cart->billing_address->id,
- 'firstName' => $cart->billing_address->first_name,
- 'lastName' => $cart->billing_address->last_name,
- 'email' => $cart->billing_address->email,
- 'address' => $cart->billing_address->address,
- 'city' => $cart->billing_address->city,
- 'state' => $cart->billing_address->state,
- 'country' => $cart->billing_address->country,
- 'postcode' => $cart->billing_address->postcode,
- 'phone' => $cart->billing_address->phone,
- ];
- }
- if ($cart->shipping_address) {
- $data->shippingAddress = [
- 'id' => $cart->shipping_address->id,
- 'firstName' => $cart->shipping_address->first_name,
- 'lastName' => $cart->shipping_address->last_name,
- 'email' => $cart->shipping_address->email,
- 'address' => $cart->shipping_address->address,
- 'city' => $cart->shipping_address->city,
- 'state' => $cart->shipping_address->state,
- 'country' => $cart->shipping_address->country,
- 'postcode' => $cart->shipping_address->postcode,
- 'phone' => $cart->shipping_address->phone,
- ];
- }
- if ($cart->payment) {
- $data->paymentMethod = $cart->payment->method;
- $data->paymentMethodTitle = core()->getConfigData('sales.payment_methods.'.$cart->payment->method.'.title');
- }
- try {
- $taxes = collect(\Webkul\Tax\Facades\Tax::getTaxRatesWithAmount($cart, true))->map(function ($rate) {
- return core()->currency($rate ?? 0);
- })->toArray();
- $data->appliedTaxes = $taxes;
- } catch (\Exception $e) {
- $data->appliedTaxes = [];
- }
- $data->haveStockableItems = $cart->haveStockableItems();
- if ($cart->selected_shipping_rate) {
- $data->selectedShippingRate = $cart->selected_shipping_rate->method ?? null;
- $data->selectedShippingRateTitle = $cart->selected_shipping_rate->method_title ?? null;
- } else {
- $data->selectedShippingRate = null;
- $data->selectedShippingRateTitle = null;
- }
- // 积分信息:用户当前积分 + 当前购物车可获得积分
- $data->rewardPoints = 0;
- $data->earnableRewardPoints = 0;
- // 积分抵扣信息
- $data->rewardPointsUsed = (int) ($cart->reward_points_used ?? 0);
- $data->rewardPointsAmount = (float) core()->convertPrice($cart->base_reward_points_amount ?? 0);
- $data->baseRewardPointsAmount = (float) ($cart->base_reward_points_amount ?? 0);
- $data->formattedRewardPointsAmount = core()->currency($cart->base_reward_points_amount ?? 0);
- if ($cart->customer_id) {
- try {
- $rewardPointRepository = app(\Longyi\RewardPoints\Repositories\RewardPointRepository::class);
- // 用户当前积分
- $data->rewardPoints = (int) $rewardPointRepository->getCustomerPoints($cart->customer_id);
- // 当前购物车可获得积分(复用订单积分计算逻辑)
- $rule = \Longyi\RewardPoints\Models\RewardActiveRule::active()
- ->ofType(\Longyi\RewardPoints\Models\RewardActiveRule::TYPE_ORDER)
- ->first();
- if ($rule) {
- $customer = \Webkul\Customer\Models\Customer::find($cart->customer_id);
- if ($customer && $rule->isApplicableToCustomer($customer)) {
- $pointsPerCurrency = $rule->getPointsForCustomer($customer);
- if ($pointsPerCurrency > 0) {
- $data->earnableRewardPoints = (int) floor(($cart->base_grand_total ?? 0) * $pointsPerCurrency);
- }
- }
- }
- } catch (\Throwable $e) {
- // 积分计算失败不阻塞购物车接口
- report($e);
- }
- }
- return $data;
- }
- public static function collection(iterable $carts): array
- {
- $cartDataCollection = [];
- foreach ($carts as $cart) {
- $cartDataCollection[] = self::fromModel($cart);
- }
- return $cartDataCollection;
- }
- public function getItems(): ?array
- {
- return $this->items;
- }
- public function setItems(?array $items): void
- {
- $this->items = $items;
- }
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Redirect URL for payment gateway')]
- public ?string $redirectUrl = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Order ID after successful order creation')]
- public ?string $orderIncrementId = null;
- #[Groups(['query', 'mutation'])]
- #[ApiProperty(description: 'Payment transaction ID returned by gateway')]
- public ?string $paymentTransactionId = null;
- }
|