/** * Internal helper function to duplicate the variant configuration from the passed article * to the new article. * * @param integer $articleId * @param integer $newArticleId * @param integer $mailDetailId */ protected function duplicateArticleDetails($articleId, $newArticleId, $mailDetailId = null) { $article = Shopware()->Models()->find('Shopware\\Models\\Article\\Article', $newArticleId); $builder = Shopware()->Models()->createQueryBuilder(); $builder->select(array('details', 'prices', 'attribute', 'images'))->from('Shopware\\Models\\Article\\Detail', 'details')->leftJoin('details.prices', 'prices')->leftJoin('details.attribute', 'attribute')->leftJoin('details.images', 'images')->where('details.articleId = ?1'); if ($mailDetailId !== null) { $builder->andWhere('details.id = ?2'); $details = $builder->setParameter(1, $articleId)->setParameter(2, $mailDetailId)->getQuery()->getArrayResult(); } else { $details = $builder->setParameter(1, $articleId)->getQuery()->getArrayResult(); } $newArticleData = $this->getNewArticleData(); $number = $newArticleData['number']; foreach ($details as $data) { $prices = array(); $data['number'] = $number; $detail = new \Shopware\Models\Article\Detail(); foreach ($data['prices'] as $priceData) { if (empty($priceData['customerGroupKey'])) { continue; } $customerGroup = $this->getCustomerGroupRepository()->findOneBy(array('key' => $priceData['customerGroupKey'])); if ($customerGroup instanceof \Shopware\Models\Customer\Group) { $priceData['customerGroup'] = $customerGroup; $priceData['article'] = $article; $prices[] = $priceData; } } $data['prices'] = $prices; // unset configuratorOptions and images. These are variant specific and are going to be recreated later unset($data['images']); unset($data['configuratorOptions']); if (!empty($data['unitId'])) { $data['unit'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Unit', $data['unitId']); } else { $data['unit'] = null; } if (!empty($data['attribute'])) { $data['attribute']['article'] = $article; } $data['article'] = $article; $detail->fromArray($data); Shopware()->Models()->persist($detail); if ($detail->getAttribute()) { Shopware()->Models()->persist($detail->getAttribute()); } } Shopware()->Models()->flush(); $this->increaseAutoNumber($newArticleData['autoNumber'], $number); }
/** * Called when the user clicks the "generateVariants" button in the article backend module. * The function expects that an article id passed and an array with active groups passed. */ public function createConfiguratorVariantsAction() { try { //first get the id parameter of the request object $articleId = $this->Request()->getParam('articleId', 1); $groups = $this->Request()->getParam('groups'); $offset = $this->Request()->getParam('offset', 0); $limit = $this->Request()->getParam('limit', 50); //the merge type defines if all variants has to been regenerated or if only new variants will be added. //1 => Regenerate all variants //2 => Merge variants $mergeType = $this->Request()->getParam('mergeType', 1); /**@var $article \Shopware\Models\Article\Article*/ $article = $this->getRepository()->find($articleId); $generatorData = $this->prepareGeneratorData($groups, $offset, $limit); if ($offset === 0 && $mergeType === 1) { $this->removeAllConfiguratorVariants($articleId); } else if ($offset === 0 && $mergeType === 2) { $this->deleteVariantsForAllDeactivatedOptions($article, $generatorData['allOptions']); } $detailData = $this->getDetailDataForVariantGeneration($article); $configuratorSet = $article->getConfiguratorSet(); $dependencies = $this->getRepository()->getConfiguratorDependenciesQuery($configuratorSet->getId())->getArrayResult(); $priceSurcharges = $this->getRepository()->getConfiguratorPriceSurchargesQuery($configuratorSet->getId())->getArrayResult(); if (empty($generatorData)) { return; } $sql = $generatorData['sql']; $originals = $generatorData['originals']; $variants = Shopware()->Db()->fetchAll($sql); if ($mergeType === 1) { $counter = $offset; } else { $sql= "SELECT COUNT(id) FROM s_articles_details WHERE articleID = ?"; $counter = Shopware()->Db()->fetchOne($sql, array($articleId)); } $allOptions = $this->getRepository()->getAllConfiguratorOptionsIndexedByIdQuery()->getResult(); //iterate all selected variants to insert them into the database foreach($variants as $variant) { $variantData = $this->prepareVariantData($variant, $detailData, $counter, $dependencies, $priceSurcharges, $allOptions, $originals, $article, $mergeType); if ($variantData === false) { continue; } //merge the data with the original main detail data $data = array_merge($detailData, $variantData); if(!$counter) { $detail = $article->getMainDetail(); } else { $detail = new \Shopware\Models\Article\Detail(); } $detail->fromArray($data); $detail->setArticle($article); Shopware()->Models()->persist($detail); $counter++; } Shopware()->Models()->flush(); $article = $this->getArticle($articleId); $this->View()->assign(array( 'success' => true, 'data' => $article )); } catch (Exception $e) { $this->View()->assign(array( 'success' => true, 'message' => $e->getMessage() )); } }