Hi zusammen,
folgender Controller funktionierte in Shopware 4 noch einwandfrei:
class Shopware_Controllers_Frontend_CbeVariantsDetail extends Enlight_Controller_Action
{
protected $mediaRepository = null;
/**
* Get a reference to the sArticle module
*
* @return sArticles
*/
private function getArticleModule() {
return Shopware()->Modules()->Articles();
}
/**
* Get the media repository
*
* @return \Shopware\Models\Media\Repository
*/
private function getMediaRepository()
{
if ($this->mediaRepository === null) {
$this->mediaRepository = Shopware()->Models()->getRepository('Shopware\Models\Media\Media');
}
return $this->mediaRepository;
}
/**
* Returns the media album used for articles
*
* @return \Shopware\Models\Media\Album|null
*/
private function getArticleAlbum()
{
return $this->getMediaRepository()
->getAlbumWithSettingsQuery(-1)
->getOneOrNullResult();
}
/**
* Get the configured number of variants from database
*/
public function getVariantsAction() {
Shopware()->Plugins()->Controller()->ViewRenderer()->setNoRender();
$config = Shopware()->Plugins()->Frontend()->CbeVariantsDetail()->Config();
$articleId = $this->Request()->getParam('articleId');
$selectedValue = $this->Request()->getParam('selectedValue');
list($variants) = $this->getArticleVariants($articleId, $selectedValue);
// echo "";
// print_r($variants);
// echo "";
$selectedarray = array();
$allcolors = array();
$allcolornames = array();
if ($config->lastSegment == "2") {
$colornumber = 2;
} else {
$colornumber = $config->colorNumber;
}
foreach($variants as $key => $value) {
$selected = explode($config->colorSeparator, $value['number']);
$color = trim(strtolower($selected[0]. $config->colorSeparator . $selected[$colornumber - 1]));
if(!in_array($color, $allcolors) && $color != NULL) {
$allcolors[] = $color;
}
}
foreach($variants as $key => $value) {
$colornames = $value['configuratorOptions'][0]['name'];
if(!in_array($colornames, $allcolornames) && $colornames != NULL) {
$allcolornames[] = $colornames;
}
}
if (!empty($selectedValue)) {
$selectedValue = explode($config->colorSeparator, utf8_encode($selectedValue));
$selectedValue = $selectedValue[1];
} else {
// get sizes from first color
$selectedValue = explode($config->colorSeparator, utf8_encode($allcolors[0]));
$selectedValue = $selectedValue[1];
}
list($selectedvariants) = $this->getArticleVariants($articleId, $selectedValue);
$json = json_encode(array(
'colors' => $allcolors,
'colornames' => $allcolornames,
'variants' => $selectedvariants
));
echo $json;
}
/**
* Helper method which returns a Query object in order to select the required variant data
*
* @param $articleId
* @param $selectedValue
* @return Doctrine\ORM\Query
*/
protected function getVariantsQuery($articleId, $selectedValue)
{
$config = Shopware()->Plugins()->Frontend()->CbeVariantsDetail()->Config();
$config->colorSeparator;
/** @var $article Shopware\Models\Article\Article */
/*$article = Shopware()->Models()->find(
'\Shopware\Models\Article\Article', $articleId
);*/
$builder = Shopware()->Models()->createQueryBuilder();
$builder->select(array('partial details.{id, articleId, number, active, inStock, releaseDate}', 'partial options.{id, groupId, name, position}' , 'partial prices.{id, price}'))
->from('Shopware\Models\Article\Detail', 'details')
->leftJoin('details.configuratorOptions', 'options')
->leftJoin('details.prices', 'prices')
->where('details.articleId = :articleId')
->andWhere('details.active = true')
->setParameters(array('articleId' => $articleId));
//die( $builder->getQuery()->getSQL());
if (!empty($selectedValue)) {
if ($config->lastSegment == "1") {
$builder->andWhere('details.number LIKE ?1')->setParameter('1', '%' . $config->colorSeparator . $selectedValue . $config->colorSeparator . '%');
}
if ($config->lastSegment == "2") {
$builder->andWhere('details.number LIKE ?1')->setParameter('1', '%' . $config->colorSeparator . $selectedValue);
}
}
//add order by conditions for the correct display order
$builder->addOrderBy('options.groupId', 'ASC');
//$builder->addOrderBy('details.number', 'ASC');
$builder->addOrderBy('details.position', 'ASC');
$builder->addOrderBy('details.id', 'ASC');
return $builder->getQuery();
}
/**
* Helper method which gets variants for a given articleId and populates the output array with cover images
*
* @param $articleId
* @return array
*/
public function getArticleVariants($articleId, $selectedValue)
{
// Get default article album
$articleAlbum = $this->getArticleAlbum();
// Get variants for the article
$query = $this->getVariantsQuery($articleId, $selectedValue);
$query->getArrayResult();
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$variants = $paginator->getIterator()->getArrayCopy();
// Get cover image for each variant
foreach($variants as &$variant) {
$variant['cover'] = $this->getArticleModule()->getArticleCover(
$articleId,
$variant['number'],
$articleAlbum
);
}
return array($variants);
}
}
Habt Ihr ne Idee, was sich hier konkret von Shopware 4 auf 5 geändert haben könnte?
Vielen Dank und viele Grüße,
Chris