Skip to content

Commit

Permalink
Merge pull request bagisto#7701 from amit-webkul/2.x-minicart-and-car…
Browse files Browse the repository at this point in the history
…t-page-development

Moved cart page coupon in API
  • Loading branch information
jitendra-webkul committed Jun 27, 2023
2 parents 0b6463f + d7e55bd commit 628cc46
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace Webkul\Checkout\Repositories;

use Webkul\Core\Eloquent\Repository;
use Webkul\Checkout\Contracts\CartItem;

class CartItemRepository extends Repository
{
/**
* Specify Model class name
*
* @return string
*/
function model(): string
public function model(): string
{
return 'Webkul\Checkout\Contracts\CartItem';
}
Expand All @@ -25,4 +22,4 @@ public function getProduct($cartItemId)
{
return $this->model->find($cartItemId)->product->id;
}
}
}
55 changes: 55 additions & 0 deletions packages/Webkul/Shop/src/Http/Controllers/API/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,59 @@ public function update(): JsonResource
]);
}
}

/**
* Apply coupon to the cart.
*/
public function storeCoupon(): JsonResource
{
$couponCode = request()->input('code');

try {
if (strlen($couponCode)) {
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $couponCode);

if ($coupon->cart_rule->status) {
if (Cart::getCart()->coupon_code == $couponCode) {
return new JsonResource([
'data' => new CartResource(Cart::getCart()),
'message' => trans('shop::app.checkout.cart.coupon-already-applied'),
]);
}

Cart::setCouponCode($couponCode)->collectTotals();

if (Cart::getCart()->coupon_code == $couponCode) {
return new JsonResource([
'data' => new CartResource(Cart::getCart()),
'message' => trans('shop::app.checkout.cart.coupon.success-apply'),
]);
}
}
}

return new JsonResource([
'data' => new CartResource(Cart::getCart()),
'message' => trans('shop::app.checkout.cart.not-found'),
]);
} catch (\Exception $e) {
return new JsonResource([
'data' => new CartResource(Cart::getCart()),
'message' => trans('shop::app.checkout.cart.coupon.error'),
]);
}
}

/**
* Remove applied coupon from the cart.
*/
public function destroyCoupon(): JsonResource
{
Cart::removeCouponCode()->collectTotals();

return new JsonResource([
'data' => new CartResource(Cart::getCart()),
'message' => trans('shop::app.checkout.cart.coupon.remove'),
]);
}
}
65 changes: 2 additions & 63 deletions packages/Webkul/Shop/src/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@

namespace Webkul\Shop\Http\Controllers;

use Illuminate\Http\Resources\Json\JsonResource;
use Webkul\Checkout\Facades\Cart;
use Webkul\CartRule\Repositories\CartRuleCouponRepository;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Customer\Repositories\WishlistRepository;

class CartController extends Controller
{
/**
* Create a new controller instance.
*
* @param \Webkul\CartRule\Repositories\CartRuleCouponRepository $cartRuleCouponRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Customer\Repositories\WishlistRepository $wishlistRepository
* @return void
*/
public function __construct(
protected CartRuleCouponRepository $cartRuleCouponRepository,
protected ProductRepository $productRepository,
protected WishlistRepository $wishlistRepository,
) {}
) {
}

/**
* Cart page.
Expand All @@ -33,54 +22,4 @@ public function index()
{
return view('shop::checkout.cart.index');
}

/**
* Apply coupon to the cart.
*/
public function storeCoupon(): JsonResource
{
$couponCode = request()->input('code');

try {
if (strlen($couponCode)) {
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $couponCode);

if ($coupon->cart_rule->status) {
if (Cart::getCart()->coupon_code == $couponCode) {
return new JsonResource([
'message' => trans('shop::app.checkout.cart.coupon-already-applied'),
]);
}

Cart::setCouponCode($couponCode)->collectTotals();

if (Cart::getCart()->coupon_code == $couponCode) {
return new JsonResource([
'message' => trans('shop::app.checkout.cart.coupon.success-apply'),
]);
}
}
}

return new JsonResource([
'message' => trans('shop::app.checkout.cart.coupon-already-applied'),
]);
} catch (\Exception $e) {
return new JsonResource([
'message' => trans('shop::app.checkout.cart.coupon.success-apply'),
]);
}
}

/**
* Remove applied coupon from the cart.
*/
public function destroyCoupon(): JsonResource
{
Cart::removeCouponCode()->collectTotals();

return new JsonResource([
'message' => trans('shop::app.checkout.cart.coupon.remove'),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Webkul\Shop\Http\Controllers\Customer\Account;

use Webkul\Core\Traits\PDFHandler;
use Webkul\Shop\Http\Controllers\Controller;
use Webkul\Sales\Repositories\InvoiceRepository;
use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Shop\Http\Controllers\Controller;

class OrderController extends Controller
{
Expand All @@ -14,8 +14,6 @@ class OrderController extends Controller
/**
* Create a new controller instance.
*
* @param \Webkul\Sales\Repositories\OrderRepository $orderRepository
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
* @return void
*/
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function toArray($request)
$productTypeInstance = $this->getTypeInstance();

$reviewHelper = app('Webkul\Product\Helpers\Review');

return [
'id' => $this->id,
'name' => $this->name,
Expand Down
18 changes: 8 additions & 10 deletions packages/Webkul/Shop/src/Resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@
],

'carousel' => [
'view-all' => 'View All'
'view-all' => 'View All',
],
],

'range-slider' => [
'range' => 'Range:'
'range' => 'Range:',
],
],

Expand Down Expand Up @@ -352,8 +352,8 @@
],
],

'view' => [
'type' => [
'view' => [
'type' => [
'bundle' => [
'none' => 'None',
],
Expand All @@ -365,7 +365,7 @@
],

'grouped' => [
'name' => 'Name'
'name' => 'Name',
],
],
],
Expand All @@ -383,8 +383,8 @@
],

'view' => [
'empty' => 'No products available in this category',
'load-more' => 'Load More',
'empty' => 'No products available in this category',
'load-more' => 'Load More',
],
],

Expand Down Expand Up @@ -445,6 +445,7 @@
'code' => 'Coupon code',
'applied' => 'Coupon applied',
'apply' => 'Apply Coupon',
'error' => 'Something went wrong',
'remove' => 'Remove Coupon',
'invalid' => 'Coupon code is invalid.',
'discount' => 'Coupon Discount',
Expand Down Expand Up @@ -489,9 +490,6 @@

'security-warning' => 'Suspicious activity found!!!',




'invalid_vat_format' => 'The given vat id has a wrong format',
'nothing-to-delete' => 'Nothing to delete',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
self.disableButton = true;
axios.post('{{ route('shop.checkout.cart.coupon.apply') }}', {code: self.couponCode})
axios.post('{{ route('shop.api.checkout.cart.coupon.apply') }}', {code: self.couponCode})
.then(function(response) {
if (response.data.success) {
self.$emit('onApplyCoupon');
Expand Down Expand Up @@ -102,7 +102,7 @@
if (self.removeIconEnabled) {
self.removeIconEnabled = false;
axios.delete('{{ route('shop.checkout.cart.coupon.remove') }}')
axios.delete('{{ route('shop.api.checkout.cart.coupon.remove') }}')
.then(function(response) {
self.$emit('onRemoveCoupon')
Expand Down
Loading

0 comments on commit 628cc46

Please sign in to comment.