/** * @param string $sku * @param Request $request * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException * * @return \Illuminate\Http\RedirectResponse */ public function putProductCategory(string $sku, Request $request) { /** @var Product $product */ $product = $this->product->where('sku', '=', $sku)->firstOrFail(); $category = $this->category->findOrFail($request->get('category-id')); $product->category()->associate($category); $product->save(); $this->webUi->successMessage("Set category for `{$product->sku}` to `{$category->name}`."); return $this->webUi->redirect('products.show', [$product->sku]); }
/** * @param Urlset $urlSet * @param Product $product */ private function addProduct(Urlset $urlSet, Product $product) { $url = new Url(htmlspecialchars($product->url())); $url->setLastMod($product->updated_at->toW3cString()); $url->setChangeFreq('weekly'); $url->setPriority(0.7); foreach ($product->images as $image) { $url->addSubElement(new Image($image->sizeUrl())); } $urlSet->addUrl($url); }
/** * @param Product $product */ private function addProductOption(Product $product) { $productOption = new ProductOption(['label' => ucfirst($this->faker()->unique()->word)]); $product->options()->save($productOption); // Add stock items for the product option. $this->repeat(function () use($productOption) { $productOption->stockItems()->save(new StockItem()); }, random_int(5, 10)); $imagesIDs = []; foreach ($this->makeImages() as $image) { $imagesIDs[] = $image->id; } $productOption->images()->attach($imagesIDs); $productOption->save(); }
/** * @return Collection|Product[] */ private function products() : Collection { if ($this->products === null) { $this->products = Product::all(); } return $this->products; }
/** * @return Collection */ private function productResourceWillLoadCollection() { /** @var Collection $collection */ $collection = new Collection(); $this->productResource->shouldReceive('orderBy->has->with->paginate')->once()->andReturn($collection); return $collection; }
/** * Create a variant for each product that does not have any. */ public function initialiseProductOptions() { /** @var Product $product */ foreach (Product::has('options', '<', 1)->get() as $product) { echo "Creating option for product {$product->id}\n"; $product->options()->save(new ProductOption(['label' => 'Standard'])); } }
/** * Pressing the delete button should delete the product. */ public function testDeleteButtonDeletesProduct() { $product = $this->createProduct(); $this->actingAs($this->staffUser())->visit(route('products.show', ['SKU' => $product->sku]))->press('Delete')->seePageIs(route('products.index')); /** @var Product $deletedProduct */ $deletedProduct = Product::onlyTrashed()->where('id', '=', $product->id)->first(); $this->assertEquals($product->id, $deletedProduct->id); $this->assertTrue($deletedProduct->trashed()); }
/** * @param string $sku * @param int $units * @param int $subunits * * @return bool */ public function setPriceBySku(string $sku, int $units, int $subunits) { /** @var Product $product */ $product = $this->productResource->where('sku', $sku)->with('prices')->limit(1)->first(); $price = $product->prices()->firstOrNew([]); $price->setAttribute('units', $units); $price->setAttribute('subunits', $subunits); $price->setAttribute('currency', 'GBP'); return $price->save(); }
/** * Should be able to set a product's tags from its view page. */ public function testCanSetTagsForProductFromProductView() { $tag = $this->createTag(); $product = $this->createProduct(); $productView = route('products.show', ['sku' => $product->sku]); $this->actingAs($this->staffUser())->visit($productView)->seePageIs($productView)->select($tag->id, 'tag-ids')->press('Save tags')->see('Tags updated'); $tagOption = $this->crawler()->filter("#tag-option-{$tag->id}"); $this->assertEquals($tagOption->attr('selected'), 'selected'); $product = Product::where('id', '=', $product->id)->with('tags')->first(); $this->assertContains($tag->id, $product->tags->pluck('id')); }
/** * Should be able to build and access a valid XML sitemap. * * @slowThreshold 10000 */ public function testSiteMap() { Artisan::call('sitemap:build'); $siteMapContent = $this->getSiteMap(); $xml = new DOMDocument(); $xml->load($siteMapContent); $errors = libxml_get_errors(); $this->assertEmpty($errors, print_r($errors, true)); foreach (Product::all() as $product) { $this->assertContains($product->url(), $siteMapContent, 'Sitemap should contain all product URLs.'); } }
/** * @param int $offerId * @param string $slug * * @throws ModelNotFoundException * * @return View|RedirectResponse */ public function view(int $offerId, string $slug) { /** @var Offer $offer */ $offer = Offer::findOrFail($offerId); if ($offer->slug() !== $slug) { return $this->webUi->redirect('offer.show', [$offerId, $offer->slug()]); } $products = Product::whereHas('offers', function ($query) use($offer) { /* @var Builder $query */ $query->where('id', '=', $offer->id); })->with(Product::standardRelations())->paginate(); return $this->webUi->view('sales::offer.view', compact('offer', 'products')); }
/** * @param int $id * @param string $slug * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException * * @return View|RedirectResponse */ public function viewCategory(int $id, string $slug) { /** @var Category $category */ $category = $this->category->findOrFail(Category::privateId($id)); if ($category->slug() !== $slug) { return $this->webUi->redirect('categories.view', [$category->id, $category->slug()]); } $tree = $category->getDescendantsAndSelf()->load(['products' => function ($query) { /* @var Product $query */ $query->with(Product::standardRelations()); }]); return $this->webUi->view('customer.category.view', compact('category', 'tree')); }
/** * @param FileBag|UploadedFile[] $images * @param Product $product * * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException */ public function attachUploadedImagesToProduct($images, Product $product) { $product->attachImages(array_map(function (UploadedFile $image) { return $this->imageRepository->storeUploadedImage($image)->id; }, $images instanceof FileBag ? $images->all() : (array) $images)); }
/** * isStored should return true when id is set. */ public function testIsStoredIsTrueWhenIdIsPresent() { $this->product->id = abs($this->generator()->anyInteger()) + 1; $this->assertTrue($this->product->isStored()); }
/** * @param Product $product * * @return Price */ protected function createPriceForProduct(Product $product) : Price { $price = Price::fromSplit(random_int(1, 99), random_int(0, 99)); $product->prices()->save($price); return $price; }
/** * @throws \RuntimeException * * @return string */ public function twitterShareUrl() : string { return 'https://twitter.com/intent/tweet?' . http_build_query(['url' => $this->wrappedObject->url(), 'text' => $this->name(), 'via' => 'ChingShopCom']); }
/** * @param Request $request * @param string $sku * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException * * @return \Illuminate\Http\RedirectResponse */ public function putProductOffers(Request $request, string $sku) { $product = Product::where('sku', '=', $sku)->firstOrFail(); $product->offers()->sync((array) $request->get('offer-ids')); $this->webUi->successMessage("Set offers for product `{$product->sku}`."); return $this->webUi->redirect('products.show', ['sku' => $sku]); }
/** * @param $units * @param $subUnits */ private function mockProductPrice(int $units, int $subUnits) { $price = new Price(['units' => $units, 'subunits' => $subUnits]); $prices = new Collection([$price]); $this->product->shouldReceive('getAttribute')->with('prices')->andReturn($prices); }
/** * @param Product $product * @param string $label * * @return ProductOption */ public function addOptionForProduct(Product $product, string $label) { $productOption = new ProductOption(['label' => $label]); return $product->options()->save($productOption); }
/** * @param Product $product * @param array $tagIds */ public function syncProductTagIds(Product $product, array $tagIds) { $product->tags()->sync($tagIds); }
/** * @param Product $product * * @return Image */ protected function attachImageToProduct(Product $product) : Image { $image = Image::create(['alt_text' => str_random(), 'url' => $this->generator()->anySlug()]); $product->images()->attach($image->id); return $image; }
/** * Run the database seeds. * * @return void */ public function run() { factory(Offer::class)->times(7)->create()->each(function (Offer $offer) { $offer->products()->saveMany(Product::inRandomOrder()->take(3)->get()); }); }
/** * @param SearchRequest $request * * @return \Illuminate\Contracts\View\View */ public function searchAction(SearchRequest $request) { return $this->webUi->view('customer.product.search', ['query' => $request->searchQuery(), 'products' => $this->productResource->search($request->searchQuery())->paginate(self::PAGE_SIZE)]); }
/** * @param Product $product * * @throws \BadMethodCallException * * @return string */ public function productMeta(Product $product) : string { $key = "product.{$product->id}.meta"; if ($this->cacheHas($key)) { return (string) $this->cacheGet($key); } $product->loadStandardRelations(); $content = $this->viewFactory->make('customer.product.meta', compact('product'))->render(); $this->cachePut($key, $content, Carbon::now()->addDays(7)); return $content; }
/** * Bootstrap the application events. * * Here you may register any additional middleware provided with your * module with the following addMiddleware() method. You may pass in * either an array or a string. * * @return void */ public function boot() { Product::observe(ProductObserver::class); }