public function massUpdate(Request $request, Product $product) { $variantsData = $request->input('variants'); $variants = $this->variantManager->getVariantRepository()->whereProduct($product)->findAll(); foreach ($variants as $variant) { if (isset($variantsData[$variant->id])) { $variantData = $variantsData[$variant->id]; $this->variantManager->getVariantRepository()->update($variant, ['stock' => $variantData['stock'] !== "" ? $variantData['stock'] : null, 'price' => $variantData['price'] !== "" ? $variantData['price'] : null]); } } return $this->success('update'); }
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 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 handle(VariantManager $variantManager) { $variantManager->refreshVariants($this->product); }