1

How to set selected value in multiselect attribute progmatically. I tried this one but it doesn't work for me.

How to Programmatically set a Product's Multi-Select Attribute by Labels

the code on the link above doesn't stop from loading

$productObj = Mage::getModel('catalog/product')->load($product->getId());
$productObj->setData('filter_category','51,52');
$productObj->save(); 

the code above is invoke after catalog_product_save_after

dotancohen
1,1306 silver badges21 bronze badges
asked Nov 25, 2013 at 5:04
5
  • Please post the code of your implementation. Also mention where is it located. Commented Nov 25, 2013 at 7:19
  • 1
    The code looks OK. I think you should try it on the _save_before event. Commented Nov 25, 2013 at 7:32
  • @Marius but I need the updated data of the product... Commented Nov 25, 2013 at 7:38
  • 1
    @marius is right,if you want this using catalog_product_save_after then it going infinite loop Commented Jun 23, 2014 at 18:14
  • Please take your time to accept an answer if it helped you. magento.stackexchange.com/help/someone-answers Commented Jul 21, 2015 at 10:29

3 Answers 3

3

Since you want to do this in the action catalog_product_save_after you could simple update the individual product attribute and not the complete product, thus stopping the infinite loop problem.

$attrCode = 'your_attribute';
$sourceModel = Mage::getModel('catalog/product')->getResource()
 ->getAttribute($attrCode)->getSource();
$valuesText = explode(',', 'red,green,blue');
$valuesIds = array_map(array($sourceModel, 'getOptionId'), $valuesText);
$product->setData($attrCode, $valuesIds);
$product->getResource()->saveAttribute($product, $attrCode);
sv3n
11.7k7 gold badges44 silver badges75 bronze badges
answered Jun 24, 2014 at 6:46
2

As others have mentioned, the problem is that you go into an infinite loop if you save the product again in the save_after event.

  1. you should use the save_before event
  2. Don't load() the product again, $product already contains all data
  3. Don't save() as the data you set on $product will be saved after the event has finished

So the code for your new observer looks like this:

public function setFilterCategoryBeforeSave(Varien_Event_Observer $observer)
{
 $observer->getProduct()->setData('filter_category','51,52');
}

That's all.

answered Feb 16, 2015 at 12:01
-1

You can filter multiple select using finset. Below is a small example of how filter it

$collection = $this->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('visibility', $visibility)
 ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
 ->addAttributeToFilter($attribute,array('finset'=>array_search($selection,$valueId)))
 ->setPageSize($limit)
 ->load();
 Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection); 

here $selection is the value of multiple select attribute to be filtered

answered Nov 25, 2013 at 7:35
4
  • I don't understand this... please explain further so that your answer can solve my problem Commented Nov 25, 2013 at 7:56
  • from what i understand from your question you need to filter product by labels in multiple select right ?? Commented Nov 25, 2013 at 8:41
  • no I am not filtering.. I am updating a product Commented Nov 25, 2013 at 8:45
  • can you tell me what exactly you are doing ? Commented Nov 25, 2013 at 8:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.