/** * Category */ public function onRun() { // Load the current slug $this->slug = $this->property('slug') ? $this->property('slug') : $this->property('default'); // Load the category $category = CategoryModel::where('slug', $this->slug)->with('products')->isActive()->first(); // Stop here if no category was found $this->exists = (bool) $category; if (!$this->exists) { return; } // Load the category variables $this->name = $category->name; $this->description = $category->description; $this->rows = $category->arrangement_rows; $this->columns = $category->arrangement_columns; $this->pseudo = (bool) $category->pseudo; $this->isVisible = (bool) $category->isVisible; // Calculate the pagination $this->pagination = $this->calculatePagination($category); // Lastly, query the products $this->products = $category->getArrangedProducts($this->pagination['current']); // Set the isEmpty flag $this->isEmpty = count($this->products) == 0; }
public function seedProducts($products) { $categories = Category::all()->lists('id'); for ($i = 0; $i < $products; $i++) { $keys = array_rand($categories, rand(1, 2)); if (is_int($keys)) { $keys = [$keys]; } $sync = []; foreach ($keys as $key) { $sync[] = $categories[$key]; } $product = Factory::create(new Product(), ['is_enabled' => rand(0, 100) > 20]); $size = Factory::make(new Option(), ['name' => 'Size']); $size->bindSelections(['id' => [null, null, null], 'name' => ['Small', 'Medium', 'Large']]); $size->save(); $product->options()->add($size); $color = Factory::make(new Option(), ['name' => 'Color']); $color->bindSelections(['id' => [null, null, null], 'name' => ['Red', 'Green', 'Blue']]); $color->save(); $product->options()->add($color); $product->categories()->sync($sync); $inventory = Factory::create(new Inventory(), ['product_id' => $product->id, 'quantity' => rand(0, 5)]); } }
/** * Update the category tree * * @throws \AjaxException * @return void */ public function onUpdateTree() { try { Category::updateInheritanceTree(input('tree')); } catch (Exception $e) { throw new AjaxException(Lang::get('bedard.shop::lang.category.popup.failed')); } Flash::success(Lang::get('bedard.shop::lang.category.popup.success')); }
public function run() { //disable foreign key check for this connection before running seeders DB::statement('SET FOREIGN_KEY_CHECKS=0;'); // Category Seeds Category::truncate(); Category::create(['position' => 1, 'name' => 'All', 'slug' => 'all', 'description' => 'Everything in the shop', 'pseudo' => 'all', 'is_visible' => 1, 'is_active' => 1]); Category::create(['position' => 2, 'name' => 'Sale', 'slug' => 'sale', 'description' => 'Products on sale.', 'pseudo' => 'sale', 'is_visible' => 1, 'is_active' => 1]); // Enable foreign keys DB::statement('SET FOREIGN_KEY_CHECKS=1;'); }
/** * Delete list rows */ public function index_onDelete() { $successful = true; if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $recordId) { if (!($record = Category::find($recordId))) { continue; } if (!$record->delete()) { $successful = FALSE; } } } if ($successful) { Flash::success('Successfully deleted categories.'); } return $this->listRefresh(); }
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(); } }
public function test_isFiltered_and_isNotFiltered_scopes() { $normal = Factory::create(new Category()); $filtered = Factory::create(new Category()); $filter = Factory::create(new Filter(), ['category_id' => $filtered->id]); $this->assertEquals(1, Category::isNotFiltered()->count()); $this->assertEquals($normal->id, Category::isNotFiltered()->first()->id); $this->assertEquals(1, Category::isFiltered()->count()); $this->assertEquals($filtered->id, Category::isFiltered()->first()->id); }
/** * Query the visible categories and put them in order */ public function onRun() { $this->categories = CategoryModel::isVisible()->inOrder()->get(); $this->categoryCount = count($this->categories); }
/** * Finds a Category by it's slug * * @param string * @return Category * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function findBySlug($slug) { return Category::whereSlug($slug)->firstOrFail(); }
/** * Returns dropdown options for the default category * * @return array */ public function getDefaultOptions() { return CategoryModel::orderBy('name')->lists('name', 'id'); }
/** * Returns products on a specific category page * * @param \October\Rain\Database\Builder $query * @param \Bedard\Shop\Models\Category $category * @param integer $page * @return \October\Rain\Database\Builder */ public function scopeOnPage($query, Category $category, $page) { // For falsey page sizes, don't paginate if (!($size = $category->getPageSize())) { return $query; } // Count from zero $page = $page > 0 ? $page - 1 : 0; return $query->skip($page * $size)->take($size); }