Skip to content

Commit

Permalink
Merge pull request bagisto#7693 from jitendra-webkul/2.0.0
Browse files Browse the repository at this point in the history
Compare list completed
  • Loading branch information
jitendra-webkul committed Jun 23, 2023
2 parents e2cbcf2 + 6891506 commit c14c280
Show file tree
Hide file tree
Showing 30 changed files with 472 additions and 1,331 deletions.
1 change: 0 additions & 1 deletion packages/Webkul/Attribute/src/Models/AttributeFamily.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function custom_attributes()
->select('attributes.*');
}


/**
* Get all the comparable attributes which belongs to attribute family.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public function update(array $data, $id, $attribute = "id")
return $family;
}


/**
* @return array
*/
Expand All @@ -158,4 +157,18 @@ public function getPartial()

return $trimmed;
}

/**
* Get all the comparable attributes which belongs to attribute family.
*/
public function getComparableAttributesBelongsToFamily()
{
return $this->attributeRepository
->join('attribute_group_mappings', 'attribute_group_mappings.attribute_id', '=', 'attributes.id')
->select('attributes.*')
->where('attributes.is_comparable', 1)
->whereNotIn('code', ['name', 'price'])
->distinct()
->get();
}
}
164 changes: 39 additions & 125 deletions packages/Webkul/Shop/src/Http/Controllers/API/CompareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
use Cart;
use Illuminate\Http\Resources\Json\JsonResource;
use Webkul\Customer\Repositories\CompareItemRepository;
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Shop\Http\Resources\CompareResource;
use Webkul\Shop\Http\Resources\CompareItemResource;

class CompareController extends APIController
{
/**
* Create a new controller instance.
*
* @param \Webkul\Customer\Repositories\CompareItemRepository $compareItemRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @return void
*/
public function __construct(
protected CompareItemRepository $compareItemRepository,
protected ProductRepository $productRepository,
protected WishlistRepository $wishlistRepository
protected ProductRepository $productRepository
) {
}

Expand All @@ -30,27 +30,23 @@ public function __construct(
*/
public function index(): JsonResource
{
$productIds = request()->input('product_ids') ?? [];

/**
* This will handle for customers.
*/
if (auth()->guard('customer')->check()) {
$compareItem = $this->compareItemRepository->get();

return CompareResource::collection($compareItem);
if ($customer = auth()->guard('customer')->user()) {
$productIds = $this->compareItemRepository
->findByField('customer_id', $customer->id)
->pluck('product_id')
->toArray();
}

/**
* This will handle for guest users.
*/
if ($productIds = request()->input('product_ids')) {
$products = $this->productRepository->whereIn('id', $productIds)->get();
$products = $this->productRepository
->whereIn('id', $productIds)
->get();

return CompareResource::collection($products);
}

return new JsonResource([
'message' => trans('shop::app.compare.products-not-available'),
]);
return CompareItemResource::collection($products);
}

/**
Expand All @@ -77,135 +73,53 @@ public function store(): JsonResource
]);

return new JsonResource([
'message' => trans('shop::app.compare.item-add'),
'message' => trans('shop::app.compare.item-add-success'),
]);
}

/**
* Method for compare items to delete products in comparison.
* Method to remove the item from compare list.
*
* @return \Illuminate\Http\Resources\Json\JsonResource
*/
public function destroy(): JsonResource
{
$compareItem = $this->compareItemRepository->deleteWhere([
'product_id' => request()->input('product_id'),
$success = $this->compareItemRepository->deleteWhere([
'product_id' => request()->input('product_id'),
'customer_id' => auth()->guard('customer')->user()->id,
]);

$compareData = $this->compareItemRepository->get();

if ($compareItem) {
if (! $success) {
return new JsonResource([
'data' => CompareResource::collection($compareData),
'message' => trans('shop::app.compare.success'),
'message' => trans('shop::app.compare.remove-error'),
]);
}

return new JsonResource([
'message' => trans('shop::app.compare.error'),
'data' => CompareResource::collection($this->compareItemRepository->get()),
'message' => trans('shop::app.compare.remove-success'),
]);
}

/**
* Method for compare items move to cart products from comparison.
*/
public function moveToCart(): JsonResource
{
try {
$customer = auth()->guard('customer')->user();

$productId = request()->input('product_id');

$data = request()->all();

$data['customer_id'] = $customer ? $customer->id : null;

$cart = Cart::addProduct($productId, $data);

if (
is_array($cart)
&& isset($cart['warning'])
) {
return new JsonResource([
'message' => $cart['warning'],
]);
}

if ($cart) {
if ($customer) {
$this->compareItemRepository->deleteWhere([
'product_id' => $productId,
'customer_id' => $customer->id,
]);
}

return new JsonResource([
'message' => trans('shop::app.compare.item-add-to-cart'),
]);
}
} catch (\Exception $exception) {
return new JsonResource([
'message' => $exception->getMessage(),
]);
}
}

/**
* Method for compare items move to wishlist products from comparison.
* Method for remove all items from compare list
*
* @return \Illuminate\Http\Resources\Json\JsonResource
*/
public function moveToWishlist(): JsonResource
public function destroyAll(): JsonResource
{
try {
$product = $this->productRepository->find(request()->input('product_id'));

if (! $product) {
return new JsonResource([
'message' => trans('shop::app.compare.product-removed'),
]);

} elseif (
! $product->status
|| ! $product->visible_individually
) {
return new JsonResource([
'message' => trans('shop::app.compare.check-product-visibility'),
]);
}

$data = [
'channel_id' => core()->getCurrentChannel()->id,
'product_id' => $product->id,
'customer_id' => auth()->guard('customer')->user()->id,
];

if (
$product->parent
&& $product->parent->type !== 'configurable'
) {
$product = $this->productRepository->find($product->parent_id);

$data['product_id'] = $product->id;
}

if (! $this->wishlistRepository->findOneWhere($data)) {
$this->wishlistRepository->create($data);

$compareItem = $this->compareItemRepository->deleteWhere([
'product_id' => $product->id,
]);

return new JsonResource([
'data' => CompareResource::collection($this->compareItemRepository->get()),
'message' => trans('shop::app.compare.wishlist-success'),
]);
}
$success = $this->compareItemRepository->deleteWhere([
'customer_id' => auth()->guard('customer')->user()->id,
]);

if (! $success) {
return new JsonResource([
'data' => CompareResource::collection($this->compareItemRepository->get()),
'message' => trans('shop::app.compare.alredy-in-wishlist'),
]);
} catch (\Exception $exception) {
return new JsonResource([
'message' => $exception->getMessage(),
'message' => trans('shop::app.compare.remove-error'),
]);
}

return new JsonResource([
'message' => trans('shop::app.compare.remove-all-success'),
]);
}
}
3 changes: 3 additions & 0 deletions packages/Webkul/Shop/src/Http/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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(
Expand Down
16 changes: 14 additions & 2 deletions packages/Webkul/Shop/src/Http/Controllers/CompareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
namespace Webkul\Shop\Http\Controllers;

use Webkul\Shop\Http\Controllers\Controller;
use Webkul\Attribute\Repositories\AttributeFamilyRepository;

class CompareController extends Controller
{
/**
* Create a new controller instance.
*
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @return void
*/
public function __construct(protected AttributeFamilyRepository $attributeFamilyRepository) {

}

/**
* Address route index page.
*
* @return \Illuminate\View\View
*/
public function index()
{
return view('shop::compare.index');
}
$comparableAttributes = $this->attributeFamilyRepository->getComparableAttributesBelongsToFamily();

return view('shop::compare.index', compact('comparableAttributes'));
}
}
51 changes: 51 additions & 0 deletions packages/Webkul/Shop/src/Http/Resources/CompareItemResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Webkul\Shop\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Webkul\Attribute\Repositories\AttributeFamilyRepository;

class CompareItemResource extends JsonResource
{
/**
* Contains comparable attributes.
*
* @var array
*/
protected static $comparableAttributes = [];

/**
* Create a new anonymous resource collection.
*
* @param mixed $resource
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public static function collection($resource)
{
self::$comparableAttributes = app(AttributeFamilyRepository::class)->getComparableAttributesBelongsToFamily();

return parent::collection($resource);
}

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$data = (new ProductResource($this->resource))
->toArray($this->resource);

foreach (self::$comparableAttributes as $attribute) {
if (in_array($attribute->code, ['name', 'price'])) {
continue;
}

$data[$attribute->code] = $this->{$attribute->code};
}

return $data;
}
}
24 changes: 0 additions & 24 deletions packages/Webkul/Shop/src/Http/Resources/CompareResource.php

This file was deleted.

Loading

0 comments on commit c14c280

Please sign in to comment.