Question

I'm building a bookshop with Magento. I've some multiple select custom attributes for Authors and Translators.

I'd like to render their values as a link to the advanced search to list all products for that Author / Translator.

  • First Problem: The advanced search does not accept the string /catalogsearch/advanced/result/?author="Name"
  • Second Problem: Can I render every item as a Link? for eg. something like this

< a href = "/catalogsearch/advanced/result/?author= < ? php echo $_product->getAttributeText('author') ?>"> < ? php echo $_product->getAttributeText('author') ?>

Was it helpful?

Solution

If I understand you correctly, your problem is now with multiple select attributes. If you execute an advanced search and look at the resulting URL, you will see it looks like:

[YOUR_BASE_URL]/catalogsearch/advanced/result/?author[]=1&author[]=2&other_attribute[]=45

Actually, this is how you transfer multiple input values with the same name. The name is then not name="author", but name="author[]". Also, make sure you don't use getAttributeText() for the URL part - it expects the option_id, so getData('author') or the magic getter getAuthor() is correct.

Your final link would be like:

<a href="<?php echo Mage::getBaseUrl() ?>catalogsearch/advanced/result/?author[]=<?php
    echo $_product->getAuthor() ?>"><?php
        echo $_product->getAttributeText('author') ?></a>

Now to the problem if a product has more than one author. As Fabian suggested, you can use is_array and foreach.

<?php

$_authors = $_product->getAuthor();
if( !is_array($_authors) ) {
    $_authors = explode(',', $_authors);
}
$_authorTexts = $_product->getAttributeText('author');
if( !is_array($_authorTexts) ) {
    $_authorTexts = array($_authorTexts);
}
foreach($_authors as $i => $_author) {
    ?><a href="<?php echo Mage::getBaseUrl() ?>catalogsearch/advanced/result/?author[]=<?php
        echo $_author ?>"><?php
            echo $_authorTexts[$i] ?></a><?php
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top