Exemplo n.º 1
0
 protected function assignAttributesToProduct(Product $product, $attributeOptions = null)
 {
     /** @var Shop $seller */
     $seller = $product->getSeller();
     if (!$seller->hasAttributeGroups()) {
         $this->seedAttributes($seller);
     }
     $attributeOptions = $attributeOptions ?: $seller->attributeGroups()->with('attributeOptions')->get()->pluck('attributeOptions')->flatten(1)->pluck('id')->toArray();
     $product->setAttributeOptions($attributeOptions, true);
     app(VariantManager::class)->refreshVariants($product);
 }
 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');
     });
 }