0

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') ?>

  • Hi domfitty, what have you tried and what doesn't work as expected? What you want to achieve sounds reasonable and shouldn't be any problem? – Fabian Blechschmidt Jul 28 '15 at 18:17
  • First problem is solved :) For single select attributes I'm able to get it as a link using this code <a href="/catalogsearch/advanced/result/?author=<?php echo $_product->getData('author') ?>"><?php echo $_product->getAttributeText('author') ?></a> I'm not able to parse it if I select 2 or more options because I get an array. Any ideas? – domfitty Jul 28 '15 at 21:23
  • what about is_array and foreach? – Fabian Blechschmidt Jul 29 '15 at 6:45
  • Maybe foreach is the solution... but I don't know hot :-P – domfitty Jul 29 '15 at 6:53
0

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
}
  • You are quite right! I'm trying to use multiple select attributes in product detail and link them INDIVIDUALLY to advanced search page. For example: Book1 with Author1, Author2 --> Link1 to advanced search of Author1 AND Link2 to advanced search of Author2 – domfitty Jul 29 '15 at 6:11
  • Okay, so why does my solution not work? – simonthesorcerer Jul 29 '15 at 6:53
  • Because I'd like to have one <a tag for each author and not a unique tag for ALL authors. – domfitty Jul 29 '15 at 9:16
  • Good start, but the code doesn't work :-( – domfitty Jul 29 '15 at 20:49
  • Hm, sorry, but this is not helpful... what does not work? Is there an error? Is there any output? Could you maybe post the output of var_dump($_authors) and var_dump($_authorTexts) right after their definition? – simonthesorcerer Jul 29 '15 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.