2

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?

2

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.

  • Yes, as you said I need to list brands which are configured as attributes in my case (manufacturer) in backend, but I need them to show by position. i.e. I gave 10 manufacturers the position 1, to other 10 the position 2, etc. I only want to show those that are with postion 1. I'm doing this because I run a multisite and need to have only 1 attribute (manufacturer). and in this case I'm able for site one to get only the 10 brandz with position 1 and for site 2 those with position 2 and so on... i.imgur.com/IgxNeiv.png – c163200 Mar 25 '13 at 13:35
  • ohk great!! then how you are assigning them position ? you should print the array of attribute and see is there is any key of position because you need to put a check on position. – Sarvagya Mar 25 '13 at 13:45
  • in the backend when editing the manufacturer's attribute ( Catalog > Attributes > Manage Attributes > Manufacturer > Manage Label / Options ) there's a position field along with the others. I thought to use that.. i.imgur.com/IgxNeiv.png – c163200 Mar 25 '13 at 13:49
  • May be i missed some thing you have made some custom module in which you are assigning the position to your manufacturer and your manufacturer attribute is a dropdown at admin. if it is like this then check where your position value is stored in database then after getting the values from the attribute collection we just put a check of position. – Sarvagya Mar 25 '13 at 14:12
  • yes, but if it is not used the position thing how can I separate the manufacturers from site 1 and site 2, so the manufacturers from site 1 to do not appear on site 2 and vice versa?? I don't see other ways.. – c163200 Mar 25 '13 at 14:12
0
    <?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);
  foreach ($manufacturers as $manufacturer): 
  // here you need put a check something like : 
     if ( manufacturer has position 0) :
    do ........ 
    //
   endforeach;
     ?> 

Now you just need to find out where the position thing is saved in database and it is done.

  • I found it... it is in: eav_attribute_option and it got the name of sort_order – c163200 Mar 25 '13 at 15:08
  • what should I do with it now?! – c163200 Mar 25 '13 at 15:12

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.