FleetCart/Modules/Cart/Http/Controllers/CartShippingMethodController.php

75 lines
2.2 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Cart\Http\Controllers;
2023-12-03 14:07:47 +00:00
use Exception;
2023-06-11 12:14:03 +00:00
use Modules\Cart\Facades\Cart;
2023-12-03 14:07:47 +00:00
use Illuminate\Pipeline\Pipeline;
use Modules\Coupon\Entities\Coupon;
use Modules\Coupon\Checkers\ValidCoupon;
use Modules\Coupon\Checkers\CouponExists;
use Modules\Coupon\Checkers\MinimumSpend;
use Modules\Coupon\Checkers\MaximumSpend;
use Modules\Coupon\Checkers\AlreadyApplied;
2023-06-11 12:14:03 +00:00
use Modules\Shipping\Facades\ShippingMethod;
2023-12-03 14:07:47 +00:00
use Modules\Coupon\Checkers\ExcludedProducts;
use Modules\Coupon\Checkers\ApplicableProducts;
use Modules\Coupon\Checkers\ExcludedCategories;
use Modules\Coupon\Checkers\UsageLimitPerCoupon;
use Modules\Coupon\Checkers\ApplicableCategories;
use Modules\Coupon\Checkers\UsageLimitPerCustomer;
2023-06-11 12:14:03 +00:00
class CartShippingMethodController
{
2023-12-03 14:07:47 +00:00
private array $checkers = [
CouponExists::class,
AlreadyApplied::class,
ValidCoupon::class,
MinimumSpend::class,
MaximumSpend::class,
ApplicableProducts::class,
ExcludedProducts::class,
ApplicableCategories::class,
ExcludedCategories::class,
UsageLimitPerCoupon::class,
UsageLimitPerCustomer::class,
];
2023-06-11 12:14:03 +00:00
/**
* Store a newly created resource in storage.
*
2023-12-03 14:07:47 +00:00
* @return \Modules\Cart\Cart
2023-06-11 12:14:03 +00:00
*/
public function store()
{
$shippingMethod = ShippingMethod::get(request('shipping_method'));
Cart::addShippingMethod($shippingMethod);
2023-12-03 14:07:47 +00:00
$cartWithCoupon = null;
$couponCode = request()->query('coupon_code');
if ($couponCode) {
$coupon = Coupon::findByCode($couponCode);
try {
resolve(Pipeline::class)
->send($coupon)
->through($this->checkers)
->then(function ($coupon) use (&$cartWithCoupon) {
Cart::applyCoupon($coupon);
$cartWithCoupon = json_encode(Cart::instance());
Cart::removeCoupon();
});
} catch (Exception) {
//Just suppressing the exception
}
}
return $cartWithCoupon ?? Cart::instance();
2023-06-11 12:14:03 +00:00
}
}