Skip to content

Commit

Permalink
products fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciframa committed Dec 8, 2024
1 parent 143997a commit 1ad1d3f
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Fitmo_backend/app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public function index(Request $request)
'product_states.*',
'categories.name as category_name',
'colors.*',
'categories.*',
'products.*',
'product_categories.category_id as category_id'
'product_categories.category_id as category_id',
'product_categories.product_order'
)->with(['categories', 'children' => function ($query) {
$query->select(
'prices.*',
Expand All @@ -48,22 +50,26 @@ public function index(Request $request)
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->selectRaw('(SELECT GROUP_CONCAT(image_path SEPARATOR "|") FROM images WHERE images.product_id = products.id AND images.is_main = 1) AS image_urls')
->where('products.parent_id', '!=', 0)
->where('products.isActive', 1);

->where('products.isActive', 1)
->whereNotNull('categories.id_parent');
}])
->leftJoin('prices', 'products.id', '=', 'prices.product_id')
->join('product_states', 'products.id', '=', 'product_states.product_id')
->join('product_categories', 'products.id', '=', 'product_categories.product_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->selectRaw('(SELECT GROUP_CONCAT(image_path SEPARATOR "|") FROM images WHERE images.product_id = products.id AND images.is_main = 1) AS image_urls')
->orderBy('products.created_at', 'desc')
->where('products.parent_id', 0)
->where('products.isActive', 1)
->whereNotNull('categories.id_parent')
->orderBy('categories.id', 'asc')
->orderBy('categories.id_parent', 'asc')
->orderBy('categories.childIndex', 'asc')
->orderBy('product_categories.product_order', 'asc')
->paginate(20, ['*'], 'page', $request->page ?? 1);


$products = $this->formatProducts($paginatedProducts, true);
$products = $this->formatProducts($paginatedProducts);

return response()->json([
'data' => $products,
Expand Down Expand Up @@ -92,6 +98,7 @@ public function getSingleProduct($product_url_name)
->orderBy('products.created_at', "asc")
->where("products.url_name", $product_url_name)
->where('products.isActive', 1)
->groupBy('products.id') // Ensure each product is only once in children
->get();

return $this->formatProducts($product);
Expand Down Expand Up @@ -246,6 +253,7 @@ public function formatProducts($products, $shouldWorkWithChildren = false)
}
if ($shouldWorkWithChildren && $product->children->isNotEmpty()) {
foreach ($product->children as $child) {

$child->image_urls = explode('|', $child->image_urls);
$child->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($child["category_name"])))));
$child->isMain = 0; // Activate the product
Expand Down Expand Up @@ -773,6 +781,17 @@ public function update(Request $request, $id)
}
}
}
if ($request->parent_id != 0 && $request->parent_id != null) {
// get productCategories and make them for this product aswell
ProductCategory::where('product_id', $product->id)->delete();
$productCategories = ProductCategory::where('product_id', $product->parent_id)->get();
foreach ($productCategories as $productCategory) {
$newProductCategories = new ProductCategory();
$newProductCategories->product_id = $product->id;
$newProductCategories->category_id = $productCategory->category_id;
$newProductCategories->save();
}
}
$product->parent_id = $request->parent_id;

$product->variant = $request->variant;
Expand Down

0 comments on commit 1ad1d3f

Please sign in to comment.