Created
July 16, 2018 09:21
-
-
Save vanWittlaer/92ca82c19d39ea38089af2a5459599db to your computer and use it in GitHub Desktop.
Using the addAttribute method for the ListProduct class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListProductService implements ListProductServiceInterface | |
{ | |
/** | |
* @var ListProductServiceInterface | |
*/ | |
private $service; | |
/** @var Container */ | |
private $container; | |
/** | |
* @param ListProductServiceInterface $service | |
*/ | |
public function __construct(ListProductServiceInterface $service, Container $container) | |
{ | |
$this->service = $service; | |
$this->container = $container; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function get($number, Struct\ProductContextInterface $context) | |
{ | |
$products = $this->getList([$number], $context); | |
return array_shift($products); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getList(array $numbers, Struct\ProductContextInterface $context) | |
{ | |
$products = $this->service->getList($numbers, $context); | |
foreach ($products as $key => $product) { | |
/** @var Struct\Attribute $attr */ | |
$attr = new Struct\Attribute([]); | |
# article id of canonical target | |
if ($product->getAttribute('core')->exists('attr2')) { | |
$id = Shopware()->Modules()->Articles()->sGetArticleIdByOrderNumber($product->getAttribute('core')->get('attr2')); | |
if ($id) { | |
$attr->set('canonicalTarget', (string)$id); | |
} | |
} | |
$product->addAttribute('custom', $attr); | |
# retrieve it in your tpl with {sArticle=$sArticle.attributes.custom->get('canonicalTarget')} | |
} | |
return $products; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment