/** * Returns the category's product arrangement * @param integer The page to load, zero loads all products * @param boolean Eager loads thumbnail and thumbnail_alt * @param boolean Eager loads product discounts * @return Collection Bedard\Shop\Models\Product */ public function getArrangedProducts($page = 0, $withThumbnails = TRUE, $withDiscounts = TRUE) { // Load all active and visible products if ($this->pseudo == 'all') { $products = Product::isVisible(); } else { $products = $this->pseudo == 'sale' ? Product::isDiscounted() : Product::inCategory($this->id); // Only show active and visible $products->isVisible(); } // Eager load thumbnails if ($withThumbnails) { $products->with('thumbnail')->with('thumbnail_alt'); } // Eager load discounts if ($withDiscounts) { $products->with('discounts')->with('categories.discounts'); } // Standard product arrangements if ($this->arrangement_method == 'alpha_asc') { $products->orderBy('name', 'asc'); } elseif ($this->arrangement_method == 'alpha_desc') { $products->orderBy('name', 'desc'); } elseif ($this->arrangement_method == 'newest') { $products->orderBy('created_at', 'desc'); } elseif ($this->arrangement_method == 'oldest') { $products->orderBy('created_at', 'asc'); } elseif ($this->arrangement_method == 'custom' && !empty($this->arrangement_order)) { foreach ($this->arrangement_order as $id) { $products->orderBy(DB::raw("id <> {$id}")); } } // If a page value was passed in, query only products on that page if ($page > 0) { $products->onPage($page, $this->productsPerPage); } return $products->get(); }
public function test_in_category_scope() { $parent = Factory::create(new Category(), ['is_inheriting' => true]); $child = Factory::create(new Category(), ['parent_id' => $parent->id, 'is_inheriting' => false]); $uninherited = Factory::create(new Category(), ['parent_id' => $child->id]); $orphan = Factory::create(new Category()); $filtered = Factory::create(new Category()); $filter = Factory::create(new Filter(), ['category_id' => $filtered->id, 'left' => 'base_price', 'operator' => '<', 'right' => 10]); $hiding = Factory::create(new Category(), ['is_hiding_out_of_stock' => true]); $product1 = Factory::create(new Product(), ['base_price' => 5]); $product2 = Factory::create(new Product(), ['base_price' => 10]); $product3 = Factory::create(new Product(), ['base_price' => 15]); $parent->products()->attach($product1); $child->products()->attach($product2); $uninherited->products()->attach($product3); $hiding->products()->attach($product3); $this->assertEquals(2, Product::inCategory($parent)->count()); $this->assertEquals(1, Product::inCategory($child)->count()); $this->assertEquals(1, Product::inCategory($uninherited)->count()); $this->assertEquals(0, Product::inCategory($orphan)->count()); $this->assertEquals(1, Product::inCategory($filtered)->count()); $this->assertEquals(0, Product::inCategory($hiding)->count()); $this->assertEquals($product1->id, Product::inCategory($filtered)->first()->id); }