protected function generateCartesian(Product $product) { $attributeOptions = $product->getAttributeOptions(); $attributesArray = $attributeOptions->groupBy('attribute_group_id')->map(function (Collection $attributeOptionsByGroup) { return $attributeOptionsByGroup->pluck('id'); })->toArray(); $result = []; while (list($key, $values) = each($attributesArray)) { if (empty($values)) { continue; } if (empty($result)) { foreach ($values as $value) { $result[] = [$key => $value]; } } else { $append = []; foreach ($result as &$product) { $product[$key] = array_shift($values); $copy = $product; foreach ($values as $item) { $copy[$key] = $item; $append[] = $copy; } array_unshift($values, $product[$key]); } $result = array_merge($result, $append); } } foreach ($result as &$array) { sort($array); } return collect($result); }
protected function selectRandomAttributesFromProduct(Product $product) { $attributeGroups = $product->attributeOptions()->with('attributeGroup')->get()->pluck('attributeGroup')->flatten(1)->unique(); return $attributeGroups->map(function (AttributeGroup $attributeGroup) { return $attributeGroup->getAttributeOptions()->random()->id; })->toArray(); }
private function performValidationOnAttributeGroups() { if (!$this->attributeGroups and !$this->product->hasActiveAttributeGroups()) { $this->valid(); return; } }
public function createSale(Product $product, $data = []) { if (!isset($data['price'])) { $data['price'] = $product->price - 0.01; } return $product->sales()->save(factory(Sale::class)->make($data)); }
public function findWhereProduct(Product $product) { return $this->executeCallback(get_called_class(), __FUNCTION__, ['product' => $product->getMorphString()], function () use($product) { // mimic a where clause in order to properly generate the cache hash $this->where('product', $product->getMorphString()); return $product->shippingPlans; }); }
private function checkProductHasNoActiveAttributeGroupsWhenInputHasNone() { if (!$this->attributeOption and $this->product->hasActiveAttributeGroups()) { $this->setErrorMessage('请选择需要购买商品的属性'); return false; } return true; }
public function getAttributesForProduct(Product $product) { $attributeOptions = $product->attributeOptions()->get()->groupBy('attribute_group_id'); $attributeGroups = $this->getAttributeGroupRepository()->whereIn('id', $attributeOptions->keys())->findAll()->keyBy('id'); // Recontsruct a nested {group.options} collection. return $attributeOptions->map(function ($attributeGroupOptions, $attributeGroupId) use($attributeGroups) { return $attributeGroups->get($attributeGroupId)->setRelation('attributeOptions', $attributeGroupOptions); }); }
protected function assignShippingPlanToProduct(Product $product) : ShippingPlan { $seller = $product->getSeller(); if (!$seller->hasShippingPlans()) { $this->seedShipping($seller); } $product->setShippingPlan($shippingPlan = $seller->getShippingPlans()->random()); return $shippingPlan; }
public function handle() { //Artisan::call('scout:flush', ['model' => Product::class]); // scout's own clear command only clears the currently existing products $algolia = new Algolia(config('scout.algolia.id'), config('scout.algolia.secret')); $product = new Product(); $index = $algolia->initIndex($product->searchableAs()); $index->clearIndex(); Artisan::call('scout:import', ['model' => Product::class]); }
public function update(Request $request, Product $product, Image $image) { DB::transaction(function () use($request, $product, $image) { $order = $image->order; $image->delete(); $newImage = $product->addMedia($request->file('image'))->toMediaLibrary(); $newImage->order = $order; $newImage->save(); }); $this->flashSuccess('edit'); return response('修改成功'); }
/** * @param Request $request * @param Product $product * @return \Illuminate\Http\RedirectResponse */ public function update(Request $request, Product $product) { // if a plan is selected authorize first if ($planId = $request->input('plan')) { $plan = $this->shippingPlanRepository->find($planId); $this->authorize('update', $plan); $product->shippingPlans()->sync([$planId]); } else { $product->shippingPlans()->sync([]); } return $this->success('edit'); }
public function show(Product $product, VariantManager $variantManager, AttributeManager $attributeManager) { if (!$product->isActive()) { abort(404); } $seller = $product->getSeller(); $attributes = $attributeManager->getAttributesForProduct($product); $variants = $variantManager->getVariantRepository()->whereProduct($product)->with('attributeOptions')->findAll(); // for stock calculation $product->setRelation('variants', $variants); return view('product::__front.products.show', compact('seller', 'product', 'attributes', 'variants')); }
public function store(AddItemToCartRequest $request, Product $product, StockValidator $stockValidator) { $attributes = $request->input('attributes', []); // If product has variant but the selected attributes don't match one // use the first variant by default. $variant = $product->variants()->hasAttributeOptions($attributes)->first(); if ($product->hasVariants() and !$variant) { $variant = $product->variants()->first(); } $stockValidator->validate($product, $variant, $request->input('quantity')); $this->cartManager->addItemToCart($this->user(), $product, $variant, $request->all()); Session::flash('added_to_cart', 1); return $this->success('add'); }
public function update(UpdateProductCategoriesRequest $request, Product $product, CategoryRepository $categoryRepository) { $categories = $request->input('categories') ?: []; if ($categories) { $categoryInstances = $categoryRepository->whereIn('id', $categories)->findAll(); foreach ($categoryInstances as $category) { if (!$category->isLeaf()) { throw new InvalidDataException(); } } } $product->categories()->sync($categories); return $this->success('edit'); }
public function home(SliderRepository $sliderRepository) { $products = Product::active()->take(12)->get(); $sliders = $sliderRepository->scopes('active')->findAll(); //$this->flashSuccessOverlay('祝贺你!'); return view('cms::__front.home', compact('products', 'sliders')); }
public function search(Request $request) { $term = urldecode($request->query('query')); if (!$term) { return view('search::__front.results', compact('term')); } $products = Product::search($term)->paginate(); $products->load(['categories', 'image']); return view('search::__front.results', compact('term', 'products')); }
public function assign(Request $request, Product $product, VariantManager $variantManager) { // Filter attributes to only authorized ones. $attributeOptions = $this->attributeManager->getAttributeOptionRepository()->whereHas('attributeGroup', function ($query) use($request) { $query->where([['seller_type', '=', $this->shop()->getMorphClass()], ['seller_id', '=', $this->shop()->getKey()]]); })->whereIn('id', $request->input('attributes', []))->get(); return DB::transaction(function () use($attributeOptions, $product, $variantManager) { // First we need to sync to product itself. // If there's no attribues to add simply clean up and exit. $product->setAttributeOptions($attributeOptions, true); if ($attributeOptions->isEmpty()) { $variantManager->cleanUpVariants($product); return $this->redirectBack(); } // Otherwise we need to work out the cartesian product // delete and create variants as necessary $variantManager->refreshVariants($product); return $this->success('edit'); }); }
public function refreshVariants() { $products = $this->products; if (!$products instanceof Collection) { $products = Product::whereIn('id', $products)->get(); } $variantManager = app(VariantManager::class); foreach ($products as $product) { $variantManager->refreshVariants($product); } }
public function getRelatedProducts() { $productIds = $this->join('attribute_options', 'attribute_groups.id', '=', 'attribute_options.attribute_group_id')->join('products', 'attribute_options.id', '=', 'products.id')->get(['products.id'])->pluck('id')->unique(); return Product::whereIn('id', $productIds)->get(); }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ use App\Modules\ProductModule\Entities\Product; Route::get('error/{code}', function ($code) { return response('测试', 404); }); Route::get('debug', function () { $seller = \App\Modules\ShopModule\Entities\Shop::first(); dd($seller->products()->with('sale')->toSql()); $product = \App\Modules\ProductModule\Entities\Product::inRandomOrder()->first(); dd($product->toSearchableArray()); return view('debug', ['thing' => $product->toSearchableArray()]); return view('front::auth.emails.password', compact('token', 'user')); }); Route::get('email', function () { Auth::user()->notify(new \App\Modules\AuthenticationModule\Notifications\WelcomeNotification(Auth::user())); return 'sent'; });
public function down(Product $product) { $product->deactivate(); return $this->success('edit'); }