/** * Reset pricing * * @return void */ public function syncPrices() { // Fetch all products within the scope of this discount $productIds = $this->products()->withDeferred($this->sessionKey)->lists('id'); $categoryIds = $this->categories()->withDeferred($this->sessionKey)->lists('id'); $products = Product::select('id', 'base_price')->whereIn('id', $productIds)->orWhereHas('categories', function ($category) use($categoryIds) { $category->whereIn('id', $categoryIds)->orWhere(function ($category) use($categoryIds) { $category->isInheritedBy($categoryIds); }); })->get(); // Delete the old price models and create new ones $this->prices()->delete(); foreach ($products as $product) { $this->createPrice($product); } }
public function test_select_status_scope() { $foo = Factory::create(new Product(), ['is_enabled' => false]); $bar = Factory::create(new Product()); $qux = Factory::create(new Product()); $baz = Factory::create(new Product(), ['base_price' => 5]); Factory::create(new Price(), ['product_id' => $baz->id, 'price' => 1]); Factory::create(new Inventory(), ['product_id' => $baz->id, 'quantity' => 1]); Factory::create(new Inventory(), ['product_id' => $qux->id, 'quantity' => 1]); $query = Product::select('id', 'price', 'base_price', 'inventory', 'is_enabled')->joinPrice()->joinInventory()->selectStatus()->get(); $disabled = $query->whereLoose('status', -2)->first(); $outOfStock = $query->whereLoose('status', -1)->first(); $normal = $query->whereLoose('status', 0)->first(); $discounted = $query->whereLoose('status', 1)->first(); $this->assertEquals($foo->id, $disabled->id); $this->assertEquals($bar->id, $outOfStock->id); $this->assertEquals($qux->id, $normal->id); $this->assertEquals($baz->id, $discounted->id); }