CartController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\Front;
  3. use App\Shop\Carts\Requests\AddToCartRequest;
  4. use App\Shop\Carts\Requests\UpdateCartRequest;
  5. use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;
  6. use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface;
  7. use App\Shop\ProductAttributes\Repositories\ProductAttributeRepositoryInterface;
  8. use App\Shop\Products\Product;
  9. use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface;
  10. use App\Shop\Products\Repositories\ProductRepository;
  11. use App\Shop\Products\Transformations\ProductTransformable;
  12. use Gloudemans\Shoppingcart\CartItem;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. class CartController extends Controller
  16. {
  17. use ProductTransformable;
  18. /**
  19. * @var CartRepositoryInterface
  20. */
  21. private $cartRepo;
  22. /**
  23. * @var ProductRepositoryInterface
  24. */
  25. private $productRepo;
  26. /**
  27. * @var CourierRepositoryInterface
  28. */
  29. private $courierRepo;
  30. /**
  31. * @var ProductAttributeRepositoryInterface
  32. */
  33. private $productAttributeRepo;
  34. /**
  35. * CartController constructor.
  36. * @param CartRepositoryInterface $cartRepository
  37. * @param ProductRepositoryInterface $productRepository
  38. * @param CourierRepositoryInterface $courierRepository
  39. * @param ProductAttributeRepositoryInterface $productAttributeRepository
  40. */
  41. public function __construct(
  42. CartRepositoryInterface $cartRepository,
  43. ProductRepositoryInterface $productRepository,
  44. CourierRepositoryInterface $courierRepository,
  45. ProductAttributeRepositoryInterface $productAttributeRepository
  46. ) {
  47. $this->cartRepo = $cartRepository;
  48. $this->productRepo = $productRepository;
  49. $this->courierRepo = $courierRepository;
  50. $this->productAttributeRepo = $productAttributeRepository;
  51. }
  52. /**
  53. * Display a listing of the resource.
  54. *
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function index()
  58. {
  59. $courier = $this->courierRepo->findCourierById(request()->session()->get('courierId', 1));
  60. $shippingFee = $this->cartRepo->getShippingFee($courier);
  61. return view('front.carts.cart', [
  62. 'cartItems' => $this->cartRepo->getCartItemsTransformed(),
  63. 'subtotal' => $this->cartRepo->getSubTotal(),
  64. 'tax' => $this->cartRepo->getTax(),
  65. 'shippingFee' => $shippingFee,
  66. 'total' => $this->cartRepo->getTotal(2, $shippingFee)
  67. ]);
  68. }
  69. /**
  70. * Store a newly created resource in storage.
  71. *
  72. * @param AddToCartRequest $request
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function store(AddToCartRequest $request)
  76. {
  77. $product = $this->productRepo->findProductById($request->input('product'));
  78. if ($product->attributes()->count() > 0) {
  79. $productAttr = $product->attributes()->where('default', 1)->first();
  80. if (isset($productAttr->sale_price)) {
  81. $product->price = $productAttr->price;
  82. if (!is_null($productAttr->sale_price)) {
  83. $product->price = $productAttr->sale_price;
  84. }
  85. }
  86. }
  87. $options = [];
  88. if ($request->has('productAttribute')) {
  89. $attr = $this->productAttributeRepo->findProductAttributeById($request->input('productAttribute'));
  90. $product->price = $attr->price;
  91. $options['product_attribute_id'] = $request->input('productAttribute');
  92. $options['combination'] = $attr->attributesValues->toArray();
  93. }
  94. $this->cartRepo->addToCart($product, $request->input('quantity'), $options);
  95. return redirect()->route('cart.index')
  96. ->with('message', 'Add to cart successful');
  97. }
  98. /**
  99. * Update the specified resource in storage.
  100. *
  101. * @param \Illuminate\Http\Request $request
  102. * @param int $id
  103. * @return \Illuminate\Http\Response
  104. */
  105. public function update(UpdateCartRequest $request, $id)
  106. {
  107. $this->cartRepo->updateQuantityInCart($id, $request->input('quantity'));
  108. request()->session()->flash('message', 'Update cart successful');
  109. return redirect()->route('cart.index');
  110. }
  111. /**
  112. * Remove the specified resource from storage.
  113. *
  114. * @param int $id
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function destroy($id)
  118. {
  119. $this->cartRepo->removeToCart($id);
  120. request()->session()->flash('message', 'Removed to cart successful');
  121. return redirect()->route('cart.index');
  122. }
  123. }