/** * Prepares the form widget view data */ public function prepareVars() { $sessionKey = input('sessionKey') ?: $this->sessionKey; $product = $this->model->product_id ? Product::findOrNew($this->model->product_id) : new Product(); $this->vars['selectionIds'] = $this->model->selections()->lists('id'); $this->vars['options'] = $product->options()->withDeferred($sessionKey)->get(); }
/** * Load the list scoreboard data * * @return void */ public function loadScoreboard() { $products = Product::isEnabled()->count(); $discounted = Product::discounted()->isEnabled()->count(); $meter = $products > 0 ? round($discounted / $products * 100) : 0; $this->vars['scoreboard'] = ['products' => $products, 'discounted' => $discounted, 'meter' => $meter]; }
/** * Product */ public function onRun() { // Load the product $product = ProductModel::where('slug', $this->property('slug'))->with('discounts')->with('categories.discounts')->with('inventories')->with('images')->isActive()->first(); // Check if the product was found $this->exists = (bool) $product; if (!$this->exists) { return; } // Load product variables $this->name = $product->name; $this->slug = $product->slug; $this->description = $product->description; $this->price = $product->price; $this->fullPrice = $product->fullPrice; $this->isDiscounted = $product->isDiscounted; $this->discount = $product->discount; $this->images = $product->images; $this->inStock = $product->inStock; // Check if the product has multiple inventories $this->hasMultipleInventories = count($product->inventories) > 1; // Load the inventories into their container foreach ($product->inventories as $inventory) { if (Settings::get('show_oos_inventories') || $inventory->quantity > 0) { $this->inventories[] = $inventory; } } }
/** * Load the list scoreboard data * * @return void */ public function loadScoreboard() { $total = Product::count(); $enabled = Product::isEnabled()->count(); $inStock = Product::inStock()->isEnabled()->count(); $outOfStock = Product::outOfStock()->isEnabled()->count(); $discounted = Product::discounted()->isEnabled()->count(); $averagePrice = Product::joinPrice()->isEnabled()->avg('price'); $disabled = $total - $enabled; $this->vars['scoreboard'] = ['total' => $total, 'enabled' => $enabled, 'disabled' => $disabled, 'inStock' => $inStock, 'outOfStock' => $outOfStock, 'averagePrice' => CurrencySettings::format($averagePrice)]; }
/** * Load a page of products for a given category * * @param \Bedard\Shop\Models\Category $category * @param integer $page * @param boolean $load_thumbnails * @param boolean $load_gallery * @param boolean $load_inventories * @return \October\Rain\Database\Collection */ public function getProducts(Category $category, $page = 1, $load_thumbnails = false, $load_gallery = false, $load_inventories = false) { $query = Product::joinPrice(); if ($load_inventories) { $query->joinInventory(); } if ($load_thumbnails) { $query->with('thumbnails'); } if ($load_gallery) { $query->with('images'); } return $query->isEnabled()->inCategory($category)->onPage($category, $page)->get(); }
/** * Extend the product model with a packaging field * @return [type] [description] */ public function boot() { Product::extend(function ($model) { $model->belongsTo['packaging'] = ['Bedard\\USPS\\Models\\Packaging']; }); Event::listen('backend.form.extendFields', function ($widget) { if (!$widget->getController() instanceof Products) { return; } if (!$widget->model instanceof Product) { return; } $widget->addSecondaryTabFields(['packaging_id' => ['tab' => 'bedard.shop::lang.products.details_tab', 'label' => 'Packaging', 'comment' => 'Select the smallest packaging this product can be mailed in.', 'type' => 'dropdown', 'options' => Packaging::all()->lists('name', 'id'), 'span' => 'right']]); }); }
/** * Delete list rows */ public function index_onDelete() { $successful = TRUE; if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $recordId) { if (!($record = Product::find($recordId))) { continue; } if (!$record->delete()) { $successful = FALSE; } } } if ($successful) { Flash::success('Successfully deleted products.'); } return $this->listRefresh(); }
/** * 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 run() { //disable foreign key check for this connection before running seeders DB::statement('SET FOREIGN_KEY_CHECKS=0;'); /** * CATEGORY SEEDS */ $i = 3; foreach (['Boards', 'Shirts', 'Hoodies', 'Stickers', 'Jackets'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 1, 'is_active' => 1]); $i++; } foreach (['Winter', 'DVDs'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 0, 'is_active' => 1]); $i++; } Category::create(['position' => $i, 'name' => 'Hats', 'slug' => 'hats', 'is_visible' => 1, 'is_active' => 0]); /** * PRODUCT SEEDS */ $colors = ['red', 'blue', 'green', 'black', 'orange', 'yellow', 'purple', 'white']; $products = ['shirt', 'hat', 'board', 'sticker']; $seeds = []; foreach ($products as $product) { foreach ($colors as $color) { $seeds[] = $color . ' ' . $product; } } shuffle($seeds); Product::truncate(); foreach ($seeds as $seed) { $product = Product::create(['name' => $seed, 'slug' => str_replace(' ', '-', $seed), 'full_price' => rand(10, 20), 'description' => "Some awesome {$seed}... You should totaly buy it.", 'is_active' => rand(0, 10) > 0 ? 1 : 0, 'is_visible' => rand(0, 10) > 0 ? 1 : 0]); if (strpos($seed, 'board') !== FALSE) { $product->categories()->attach(3); } elseif (strpos($seed, 'hat') !== FALSE) { $product->categories()->attach(10); } elseif (strpos($seed, 'shirt') !== FALSE) { $product->categories()->attach(4); } elseif (strpos($seed, 'sticker') !== FALSE) { $product->categories()->attach(6); } $small = Inventory::create(['product_id' => $product->id, 'name' => 'Small', 'quantity' => rand(0, 2), 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $medium = Inventory::create(['product_id' => $product->id, 'name' => 'Medium', 'quantity' => rand(0, 2), 'position' => 1, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $large = Inventory::create(['product_id' => $product->id, 'name' => 'Large', 'quantity' => rand(0, 2), 'position' => 2, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); } /** * DISCOUNT SEEDS */ // Demo category discount DB::table('bedard_shop_discounts')->insert(['name' => 'Category Discount', 'amount' => rand(10, 25), 'is_percentage' => 1]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 1, 'discountable_id' => rand(3, 6), 'discountable_type' => 'Bedard\\Shop\\Models\\Category']); DB::table('bedard_shop_discounts')->insert(['name' => 'Product Discount', 'amount' => rand(5, 8), 'is_percentage' => 0]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 2, 'discountable_id' => rand(1, 20), 'discountable_type' => 'Bedard\\Shop\\Models\\Product']); /** * PROMO CODE */ Coupon::create(['name' => 'Foo', 'message' => 'Thanks for entering "foo".', 'amount' => rand(10, 20), 'is_percentage' => 1, 'cart_value' => rand(20, 50)]); // Enable foreign keys DB::statement('SET FOREIGN_KEY_CHECKS=1;'); /** * Mock carts */ $fnames = ['John', 'Mary', 'Alex', 'Mark', 'Sally']; $lnames = ['Smith', 'Johnson', 'Jones', 'Doe']; for ($i = 0; $i < 30; $i++) { $cart = Cart::create([]); $inventories = []; for ($j = 0; $j < rand(5, 15); $j++) { $inventories[] = rand(1, 95); } foreach ($inventories as $inventory) { $item = CartItem::firstOrCreate(['cart_id' => $cart->id, 'inventory_id' => $inventory, 'quantity' => rand(1, 2)]); } $first = $fnames[rand(0, 4)]; $last = $lnames[rand(0, 3)]; $customer = Customer::firstOrCreate(['first_name' => $first, 'last_name' => $last, 'email' => strtolower("{$first}.{$last}@example.com")]); $order = Order::create(['created_at' => Carbon::now()->subDays(rand(1, 50))]); $order->customer_id = $customer->id; $order->cart_id = $cart->id; $order->amount = $cart->total; $cart->markAsComplete($order); $order->shipping_address = ['first_name' => $first, 'last_name' => $last, 'address1' => '123 Foo Street', 'city' => 'Beverly Hills', 'state' => 'CA', 'postcode' => '90210', 'country' => 'US']; $order->gateway = 'PayPal_Express'; $order->gateway_code = 'FAKE-PAYPAL-ID'; $order->save(); } }
/** * 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_on_page_scope() { $paginated = Factory::make(new Category(), ['page_rows' => 3, 'page_columns' => 4]); $query = Product::onPage($paginated, 1)->getQuery(); $this->assertEquals(0, $query->offset); $this->assertEquals(12, $query->limit); $query = Product::onPage($paginated, 2)->getQuery(); $this->assertEquals(12, $query->offset); $infinite = Factory::make(new Category(), ['page_rows' => 0, 'page_columns' => 4]); $query = Product::onPage($infinite, 1)->getQuery(); $this->assertEquals(0, $query->offset); $this->assertEquals(0, $query->limit); }
/** * Loads a product by inventory ID or product slug * @param integer $inventoryId * @param string $slug * @return Bedard\Shop\Models\Inventory */ private function loadInventory($inventoryId, $slug) { // If an inventory ID was passed in, find and return it if ($inventoryId) { return Inventory::inStock()->find($inventoryId); } // Otherwise, check the product and return it's first inventory $product = Product::where('slug', $slug)->with('inventories')->whereHas('inventories', function ($inventory) { $inventory->inStock(); })->isActive()->first(); // Grab the first inventory foreach ($product->inventories as $inventory) { return $inventory; } }
/** * Finds a Product by it's slug * * @param string * @return Product * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function findBySlug($slug) { return Product::whereSlug($slug)->with('current_price.discount', 'options.selections', 'inventories.selections', 'thumbnails', 'images')->firstOrFail(); }
/** * Validate that no inventories exist matching our selection signature * * @throws \October\Rain\Exception\ValidationException * @return void */ public function validateSelections($selections, $sessionKey = null) { $product = $this->product_id ? Product::findOrNew($this->product_id) : new Product(); $inventory = $product->inventories()->withDeferred($sessionKey)->whereHasSelections($selections); if ($this->id) { $inventory->where('id', '!=', $this->id); } if ($inventory->exists()) { $error = Lang::get('bedard.shop::lang.inventory.signature_collision'); Flash::error($error); throw new ValidationException($error); } }