Magento – Sort Manufacturer’s attribute by their “position”

attributesbackendfrontendmagento-1.7

I'm trying to sort manufacturer's attribute option values by their "position" as entered in the backend, in attributes' panel. I need to get, for example all the manufacturers with position 2, or all the manufacturers with position 1 etc., by specifying it in the code below.

I've tried ->addFieldToFilter('attribute_position', array('eq'=> 200)); in the code below, but it isn't working. (btw this is the only of the 3 codes that actually is working.)

<?php
  $product = Mage::getModel('catalog/product');
  $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
  ->setEntityTypeFilter($product->getResource()->getTypeId())
  ->addFieldToFilter('attribute_code', 'manufacturer');
  $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
  $manufacturers = $attribute->getSource()->getAllOptions(false);
?>

<div id="brandz">
<?php 
$rows = array_chunk($manufacturers, 6);
foreach ($rows as $row) {
?>

<ul class="rapper">
<?php foreach ($row as $manufacturer) { ?>
<li>
<a href=""><?php echo $manufacturer['label'] ?></a>
</li>
<?php } ?>
</ul>
<?php } ?>
</div>

I've also tried this:

<?php
$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
 ->setAttributeFilter( $attribute->getId() )
 ->setStoreFilter()
 ->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);
?>

but it isn't working, also, for me.

I was also suggested to do it this way (but it still won't work)

<?php
    $attribute = Mage::getModel('eav/entity_attribute')->load('manufacturer', 'attribute_code');

    $manufacturers = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
     ->setAttributeFilter($attribute->getId())
     ->addFieldToFilter('sort_order', 1)
     ->unshiftOrder('sort_order', 'ASC')
     ->setStoreFilter();
?>

<div id="brandz">
<?php 
    $rows = array_chunk($manufacturers, 6);
    foreach ($rows as $row) {
?>

<ul class="rapper"><?php foreach ($row as $manufacturer) { ?>
    <li><a href=""><?php echo $manufacturer['label'] ?></a></li>
<?php } ?></ul><?php } ?>
</div>

Do you have any ideas how could it be done?

Best Answer

I think i can help you here.

What i can see here is you need something like brands listing which are configured as an attribute i.e manufacturer in back-end so that when admin sets the manufacturer for the product that product appears when some one click on that manufacturer's label and before that all the manufacturers are listed.

If there is anything more tell me i will provide you the code snippet here. Also tell me how far you have reached.

Related Topic